Document directory
- Use the specialized functions provided by PHP to execute simple commands.
- 2. Use the popen () function to open the process and implement command Interaction
- Example 3
Using the specialized functions provided by PHP, you can simply execute the command prototype:
String System (string command [, int return_var]);
String exec (string command [, array $ output [, int $ return_var]);
Void passthru (string command [, int return_var]);
Differences:
System () Outputs and returns the last shell result.
Exec () does not output results. The last shell result is returned. All results can be saved to a returned array.
Passthru () only calls the command and directly outputs the command running result to the standard output device.
Similarities:
Command Execution status code.
Prerequisites:
Since PHP is basically used for web program development, security has become an important aspect of consideration. As a result, PHP designers added a security mode to PhP. If running in safe mode, the PHP script will be subject to the following four restrictions:
1) execute External commands
2) There are some restrictions when opening a file
3) connect to the MySQL database
4) HTTP-based authentication
In security mode, only external programs in a specific directory can be executed, and calls to other programs will be rejected. This directory can be specified by using the safe_mode_exec_dir command in the PHP. ini file, or by adding the -- with-Exec-Dir option to compile PHP. The default value is/usr/local/PHP/bin.
In the/etc/PHP. ini file
Set safe_mode_exec_dir =
Change
Safe_mode_exec_dir = "/wwwroot/blog/protected/commands/shell"
2. Use the popen () function to open the process and implement command Interaction
For example, when adding Linux system users, you need to call Su to change the current user to the root user, and the su command must enter the root password on the command line. As follows (the root password is root ):
<?php $sucmd = "su --login root --command"; $useraddcmd = "useradd "; $rootpasswd = "root"; $user = "wzhwho"; $useraddcmd = sprintf("%s "%s %s"",$sucmd,$useraddcmd,$user); $fp = @popen($useraddcmd,"w"); @fputs($fp,$rootpasswd); @pclose($fp); ?>
Example C program
#include <stdio.h> #include <stdlib.h>int main(int argc, char* argv[]){ int a = 0; int r =0; if(argc != 2) { printf("usage: %s &r\n", argv[0]); exit(-1); } a = atoi(argv[1]); r = a*a; printf("Ok:r = %d\n",r); return r;}
PHP Program
Output result:
===== System ========
Apache
R is Apache, RET is 0
===== Exec =========
R is Apache, RET is Array
======= Passthru ======
Apache
R is Apache, RET is 0