Use php to simulate the ping command of the commonly used doscommand. Here, the exec function is used for function explanation, and the related function system is used. Use php to simulate the ping command of the commonly used doscommand. Here, we mainly use the php built-in function exec to call the system's ping command to implement the ping command function.
The Code is as follows:
$ To_ping = 'www .phpernote.com ';
$ Count = 2;
$ Psize = 66;
Echo "the php ping command is being executed. Please wait... \ n
";
Flush ();
While (1 ){
Echo"
";
exec("ping -c $count -s $psize $to_ping", $list);
for($i=0;$i
print $list[$i]."\n";
}
echo "
";
Flush ();
Sleep (3 );
}
?>
Note that to use the exec function, the server must support calling the built-in system functions. You can also use php built-in functions such as system to implement this function. The php manual explains the two functions:
Exec --- execute external program
Syntax: string exec (string command [, array & output [, int & return_var])
Note:
Exec () executes the command, but it does not output anything. It simply transmits the last line from the command result. If you need to execute a command, you can use the passthru () function to obtain all data from the command.
If the array parameter is given, the specified array will be filled with each line output by the command. Note: If the array already contains some elements, exec () it will be appended to the back of the array. If you do not want this function to append an element, you can call unset () before passing this array to exec ().
If the array and return_var parameters are given, the status command returned for execution will be written to this variable.
Meaning: If you allow data input by a user to be passed to this function, you should use escapeshellcmd () to confirm that this user cannot cheat (trick) system to execute arbitrary (arbitrary) commands.
Note: If you use this function to start a program and want to leave it when running in the background (background), you must ensure that the output of this program is redirected) to a file or some output data streams, otherwise PHP will be suspended (hang) until the program execution ends.
System --- execute an external program and display the output
Syntax: string system (string command [, int & return_var])
Note:
System () executes the command and outputs the result. If the return_var parameter is given, the status code for executing the command will be written to this variable.
Note: If you allow data input from the user to be passed to this function, you should use escapeshellcmd () to determine that this user cannot cheat (trick) system to execute arbitrary (arbitrary) commands.
Note: If you use this function to start a program and want to leave it when running in the background (background), you must ensure that the output of this program is redirected) to a file or some output data streams, otherwise PHP will be suspended (hang) until the program execution ends.
These two methods are used to call the system shell command. The differences are as follows:
Exec can return all the execution results to the $ output function (array). $ status indicates that the execution status 0 indicates success 1 indicates failure.
Systerm does not need to provide the $ output function. It directly returns the result. Similarly, $ return_var indicates that the execution status code 0 indicates that success 1 indicates failure.
Exec example:
The Code is as follows:
$ A = exec ("dir", $ out, $ status );
Print_r ($ );
Print_r ($ out );
Print_r ($ status );
?>
System example:
The Code is as follows:
$ A = system ("dir", $ out );
Print_r ($ );
Print_r ($ out );
?>