Home  >  Article  >  Backend Development  >  How to handle when php cannot call external commands

How to handle when php cannot call external commands

WBOY
WBOYOriginal
2016-07-25 08:58:471247browse
  1. exec(""/bin/ls -l"");
  2. exec(""/bin/ls -l"", $res);
  3. #$res is a data, Each element represents a line of the result
  4. exec(""/bin/ls -l"", $res, $rc);
  5. #The value of $rc is the status code of the command /bin/ls -l. In case of success, it is usually 0
  6. ?>
Copy code

passthru() Prototype: void passthru (string command [, int return_var]) passthru() only calls the command and does not return any results, but outputs the running results of the command directly to the standard output device as is. Therefore, the passthru() function is often used to call programs like pbmplus (a tool for processing images under Unix that outputs a binary stream of original images). It can also get the status code of command execution.

Example:

  1. header(""Content-type: image/gif"");
  2. passthru(""./ppmtogif hunte.ppm"");
  3. ?>
Copy code

2, use the popen() function to open the process The above method can only simply execute the command, but cannot interact with the command. But sometimes you must enter something into the command. For example, when adding a Linux system user, you need to call su to change the current user to root, and the su command must enter the root password on the command line. In this case, it is obviously not possible to use the method mentioned above.

The

popen() function opens a process pipe to execute the given command and returns a file handle. Since a file handle is returned, you can read and write to it. In PHP3, this kind of handle can only be used in a single operation mode, either writing or reading; starting from PHP4, it can read and write at the same time. Unless the handle is opened in one mode (read or write), the pclose() function must be called to close it.

Example 1:

  1. $fp=popen(""/bin/ls -l"", ""r"");
  2. ?>
Copy code

Example 2:

  1. /* How to add a system user in PHP
  2. Add a user named james,
  3. root password is very good. For reference only
  4. */
  5. $sucommand = ""su --login root --command"";
  6. $useradd = ""useradd "";
  7. $rootpasswd = ""verygood"";
  8. $user = ""james "";
  9. $user_add = sprintf(""%s ""%s %s"""",$sucommand,$useradd,$user);
  10. $fp = @popen($user_add,""w"") ;
  11. @fputs($fp,$rootpasswd);
  12. @pclose($fp);
  13. ?>
Copy the code

3, use the backtick (`, which is under the ESC key on the keyboard That, and ~ are on the same top) The method is very simple. Use two backticks to enclose the command to be executed as an expression. The value of this expression is the result of the command execution. like:

  1. $res=`/bin/ls -l`;
  2. echo '
    '.$res.'
    ';
  3. ?>
Copy the code

The output is similar to this: hunte.gif hunte.ppm jpg.htm jpg.jpg passthru.php There are two issues to consider: security and timeouts. Let’s look at safety first. For example, you have a small online store, so the list of products available for sale is placed in a file. You write an HTML file with a form that asks your users to enter their email address and then sends them a list of products. Assuming that you have not used PHP's mail() function (or have never heard of it), you call the mail program of the Linux/Unix system to send this file. The program looks like this:

  1. system(""mail $to < products.txt"");
  2. echo ""The product catalog has been sent to your mailbox: $to"";
  3. ?>
Copy code

Using this code will not cause any danger to ordinary users, but in fact there is a very large security vulnerability. If a malicious user enters such an EMAIL address: '--bla mail someone@domain.com ) and read from file (

Timeout issue If the command to be executed takes a long time, the command should be run in the background of the system. But by default, functions such as system() wait until the command is finished running before returning (actually, they have to wait for the output of the command), which will definitely cause the PHP script to time out.

Solution: Redirect the output of the command to another file or stream, example:

  1. system(""/usr/local/bin/order_proc > /tmp/null &"");
  2. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn