Php calls the shell method, php calls the shell Method

Source: Internet
Author: User
Tags call shell

Php calls the shell method, php calls the shell Method

The example in this article describes how php calls shell and shares it with you for your reference. The specific method is as follows:

I. Configuration

Check whether the security mode is enabled in the php. ini configuration, mainly in the following three places:
Safe_mode = (if this parameter is off, you don't need to worry about it)
Disable_functions =
Safe_mode_exec_dir =

Ii. Use

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:
 
① Execute External commands
② There are some restrictions when opening a file
③ Connect to the MySQL database
④ 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.
 
If you call an external command that should be able to output the result (meaning that the PHP script is correct), the result is blank, it is very likely that your network administrator has run PHP In security mode.
 
3. How to do it?
 
You can use the following three methods to call external commands in PHP:
 
1) use special functions provided by PHP
 
PHP provides 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 the one in other languages. It executes the given command, outputs and returns the result. The second parameter is optional and is used to obtain the status code after the command is executed.
 
Example:
 
Copy codeThe Code is as follows: system ("/usr/local/bin/webalizer ");

 
Exec ()
 
Prototype: string exec (string command [, string array [, int return_var])
 
The exec () function is similar to system (). It 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, the complete result can be obtained using the second parameter array by appending the result row by row to the end of the array. Therefore, if the array is not empty, we recommend that you use unset () to clear it before calling it. Only when the second parameter is specified can the third parameter be used to obtain the command execution status code.
 
Example:
 
Copy codeThe Code is as follows: exec ("/bin/ls-l ");
Exec ("/bin/ls-l", $ res );
# $ Res is a data. Each element represents a row of the result.
Exec ("/bin/ls-l", $ res, $ rc );
# $ Rc is the status code of the command/bin/ls-l. The success is usually 0.

 
Passthru ()
 
Prototype: void passthru (string command [, int return_var])
 
Passthru () only calls the command and does not return any results, but directly outputs the running result of the command to the standard output device as is. Therefore, the passthru () function is often used to call programs such as pbmplus (a Unix-based image processing tool that outputs binary original image streams. It can also get the status code of command execution.
 
Example:
 
Copy codeThe Code is as follows: header ("Content-type: image/gif ");
Passthru ("./ppmtogif hunte. ppm ");

I hope this article will help you with PHP programming.


How to execute shell in php

Php provides the system (), exec (), passthru () functions to call external commands.
Although these three commands can execute shell commands in linux, they are actually different:
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: both of them can obtain the command execution status code.

Example: system ("/usr/local/bin/webalizer ");

How can I use PHP to call SHELL commands? After the call and execute the SHELL command?

Disable functions such as sysyem () passthru () exec.
Execute passthru ('ps-ef ') to obtain the output content of this command. Obtain the required content by regular expression, and then call these functions to execute the operation. Detailed usage manual.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.