The example in this article describes the method of calling shell in PHP and is shared with everyone for your reference. The specific method is as follows:
1. Configuration
Check whether the safe mode is turned on in the configuration in php.ini, mainly in the following three places
safe_mode = (If this is off, you don’t need to worry about the following two)
disable_functions =
safe_mode_exec_dir=
2. Use
Since PHP is basically used for WEB program development, security has become an important aspect that people consider. So PHP designers added a door to PHP: safe mode. If running in safe mode, the PHP script will be subject to the following four restrictions:
① Execute external commands
② There are some restrictions when opening files
③ Connect to MySQL database
④ HTTP-based authentication
In safe mode, only external programs in specific directories can be executed, and calls to other programs will be denied. This directory can be specified using the safe_mode_exec_dir directive in the php.ini file, or by adding the --with-exec-dir option when compiling PHP. The default is /usr/local/php/bin.
If you call an external command that should be able to output results (meaning there are no errors in the PHP script), but you get a blank, then it is likely that your network administrator has run PHP in safe mode.
3. How to do it?
To call external commands in PHP, you can use the following three methods:
1) Use the special functions provided by PHP
PHP provides a total of three specialized functions for executing external commands: system(), exec(), and passthru().
system()
Prototype: string system (string command [, int return_var])
The system() function is similar to that in other languages. It executes the given command, outputs and returns the result. The second parameter is optional and is used to get the status code after the command is executed.
Example:
exec()
Prototype: string exec (string command [, string array [, int return_var]])
The exec() function is similar to system(), and also executes the given command, but does not output the result, but returns the last line of the result. Although it only returns the last line of the command result, using the second parameter array can get the complete result by appending the results line by line to the end of the array. So if the array is not empty, it is best to use unset() to clear it before calling it. Only when the second parameter is specified, the third parameter can be used to obtain the status code of command execution.
Example:
I hope this article will be helpful to everyone’s PHP programming design.
php provides us with three functions: system(), exec(), and passthru() to call external commands.
Although these three commands can execute shell commands of the Linux system, in fact they are The difference:
system() outputs and returns the last line of shell results.
exec() does not output results and returns the last line of shell results. All results can be saved in a returned array.
passthru() only calls the command and outputs the command execution result directly to the standard output device as is.
Same point: you can get the status code of command execution
Example: system("/usr/local/bin/webalizer/webalizer");
Unblock functions such as sysyem() passthru() exec().
Execute passthru('ps -ef'); to get the output of this command. Regularly get what you need, and then call these functions to perform the operation. Please refer to the manual for specific usage.