Using PHP to simulate our usual DOS command ping command, the main use of PHP's built-in function exec to call the system's ping command, to achieve the ping command function.
<?php$to_ping= ' www.phpernote.com '; $count =2; $psize =66;echo "Executing the PHP ping command, please wait for ...\n<br><br>"; flush (); while (1) {echo "<pre>", exec ("Ping-c $count-S $psize $to _ping", $list); for ($i =0; $i <count ($list); $i + +) {
print $list [$i]. " \ n "; } echo "</pre>"; Flush (); Sleep (3);}? >
Note that using the EXEC function requires the server to support calling the system built-in functions. You can also use PHP built-in functions such as system to implement this function. The PHP manual explains these two functions:
EXEC---Execute external program
Syntax: string exec (String command [, array &output [, int &return_var]])
Exec () executes command commands given, but it does not output anything, it simply returns the last line from the result of the command, and if you need to execute a command and get all the data from the command, you can use the PassThru () function.
If a parameter array is given, the specified array will be filled with each line output by the command, note that if the array already contains some elements, exec () will append it to the array, and if you do not want this function to append elements, you can pass this array to exec () Before calling Unset ().
If a parameter array and Return_var are given, then the status command returned to the execution will be written to this variable.
Meaning: If you allow data input from the user to be passed to this function, then you should use Escapeshellcmd () to determine that the user cannot spoof (trick) the system to execute arbitrary (arbitrary) commands.
Note: If you use this function to start a program and want to leave it in the background (background) execution, you must make sure that the output of the program is redirected to a file or some output stream, otherwise PHP will hang Until the end of the program execution.
The system---Executes the external program and displays the output
Syntax: string system (String command [, int &return_var])
Description
System () executes the command given and outputs the result. If a parameter return_var is given, the execution of the command's status code will be written to this variable.
Note: If you allow data input from the user to be passed to this function, then you should use Escapeshellcmd () to determine that the user cannot spoof (trick) the system to execute arbitrary (arbitrary) commands.
Note: If you use this function to start a program and want to leave it in the background (background) execution, you must make sure that the output of the program is redirected to a file or some output stream, otherwise PHP will hang Until the end of the program execution.
These two are all used to invoke the system shell command, different points:
Exec can return all execution results to the $output function (array), $status is the execution status 0 for success 1 for failure
Systerm does not need to provide the $output function, he is directly returning the result, the same $return_var is the execution of the status code 0 for the success of 1 for the failure
exec Example:
<?php $a = exec ("dir", $out, $status); Print_r ($a); Print_r ($out); Print_r ($status); ? >
System Example:
<?php $a = System ("dir", $out); Print_r ($a); Print_r ($out); ? >