PHP Invoke Shell Method _php instance

Source: Internet
Author: User
This article describes the PHP method of invoking the shell, shared for everyone to reference. Here's how:

First, the configuration

Check whether the php.ini is configured to turn on Safe mode, mainly in the following three places
Safe_mode = (this if two off under the off-tube)
Disable_functions =
Safe_mode_exec_dir=

Second, use

Because PHP is basically used for Web program development, security has become an important aspect of people's thinking. So PHP's designers have added a door to PHP: Safe mode. If you are running in Safe mode, then the PHP script will be subject to the following four limitations:

① Executing external commands
② some restrictions when opening a file
③ connecting MySQL Database
④ HTTP-based authentication

In Safe mode, only external programs in a particular directory can be executed, and calls to other programs will be rejected. This directory can be specified in the php.ini file with the Safe_mode_exec_dir directive, or in the compiler PHP is added--with-exec-dir option, the default is/usr/local/php/bin.

If you call an external command that you should be able to output (meaning that there is no error in the PHP script), the result is a blank space, so it is likely that your network administrator has already run PHP in safe mode.

Third, how to do?

Calling external commands in PHP can be implemented in three ways:

1) Special functions provided by PHP

PHP provides a total of 3 specialized functions for executing external commands: System (), exec (), PassThru ().

System ()

Prototype: string system (String command [, int return_var])

System () The function is similar 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 executes.

Example:

Copy code code as follows: System ("/usr/local/bin/webalizer/webalizer");


EXEC ()

Prototype: string exec (String command [, string array [, int return_var]])

The exec () function, like system (), also executes the given command, but does not output the result, but instead returns the last line of the result. Although it returns only the last line of the command result, the second parameter array gives the complete result by appending the result line to the end of the array. So if the array is not empty, it is best to clear it with unset () before calling it. The third parameter can be used to obtain the status code of the command execution only if the second parameter is specified.

Example:

Copy the Code code as follows: Exec ("/bin/ls-l");
EXEC ("/bin/ls-l", $res);
# $res is a data, each element represents a row of results
EXEC ("/bin/ls-l", $res, $RC);
# The value of the $RC is the status code of the command/bin/ls-l. The case of success is usually 0


PassThru ()

Prototype: void PassThru (String command [, int return_var])

PassThru () only invokes the command, does not return any results, but outputs the result of the command directly to the standard output device. So the PassThru () function is often used to invoke programs such as Pbmplus (a tool for processing pictures under Unix, a stream of output binary raw images). It can also get the status code of the command execution.

Example:

Copy the Code code as follows: Header ("Content-type:image/gif");
PassThru ("./ppmtogif hunte.ppm");

I hope this article is helpful to everyone's PHP programming.

  • 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.