13.2 Printing the Output of a Program


You want to send the output of a program directly to standard output without any intermediate steps or extra garbage.

Technique

Use the passthru () function, which will print the results of your shell command directly to standard output:

 <?php passthru('date'); ?> 

Comments

If you simply need to execute a command and display the output to the browser, use the passthru() function, which frees you from worrying about the intermediate steps. (This is similar to fpassthru() and readfile() , which act on files.)

Gotcha

I stressed this in the previous recipe, but I will also stress it here: Always use the EscapeShellCmd() function when accepting data from your users. In that way, harmful shell characters are escaped.

 passthru(EscapeShellCmd($submitted_data)); 



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net