Using PHP to simulate our common DOS command ping command, here is the PHP built-in function called Exec to invoke the System ping command, so as to achieve the ping command function.
Copy Code code as follows:
<?php
$to _ping= ' www.phpernote.com ';
$count = 2;
$psize = 66;
echo "is 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 using the EXEC function must require server support to invoke the system's built-in functions. You can also use PHP built-in functions such as system to implement this functionality. The PHP manual explains these two functions:
EXEC---Execute an external program
syntax: string exec (String command [, array &output [, int &return_var]])
Description:
EXEC () executes the 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 row that the command outputs. Note: If the array has previously contained elements, EXEC () will append it to the array, if you do not want the function to append elements, you can pass this array to exec () Call Unset () before.
if a parameter array and Return_var are given, the state command to return execution will be written to this variable.
: If you allow data from user input to be passed to this function, then you should use Escapeshellcmd () to determine that this user cannot spoof (trick) the system to execute arbitrary (arbitrary) commands.
Note: If you use this function to start a program and you want to leave it in the background (background), you have to make sure that the output of the program is turned (redirected) to a file or some output stream, otherwise PHP will hang (hang) Until the program execution is finished.
System---Execute external programs and display output
Syntax: string system (String command [, int &return_var])
Description
System () executes the given command and outputs the result. If a parameter return_var is given, the status code executing the command will be written to this variable.
Note: If you allow data from user input to be passed to this function, then you should use Escapeshellcmd () to determine that this user cannot spoof (trick) the system to execute arbitrary (arbitrary) commands.
Note: If you use this function to start a program and you want to leave it in the background (background), you must make sure that the output of the program is turned (redirected) to a file or some output stream, otherwise PHP will hang (hang) Until the program execution is finished.
These two are all used to invoke the system shell command, different points:
EXEC can return all the results of execution to the $output function (array), $status is executed state 0 for success 1 for failure
Systerm does not need to provide the $output function, he is directly returning the result, same $return_var is executing the status code 0 for success 1 for failure
exec Example:
Copy Code code as follows:
<?php
$a = exec ("dir", $out, $status);
Print_r ($a);
Print_r ($out);
Print_r ($status);
?>
System Example:
Copy Code code as follows:
<?php
$a = System ("dir", $out);
Print_r ($a);
Print_r ($out);
?>