Anatomy using the PHP shell script _php Tutorial

Source: Internet
Author: User
PHP after a long period of development, a lot of users are very familiar with PHP, here I publish a personal understanding, and discuss with you. In most cases, I use the exec () command and the data array to handle everything. or use Shell_exec () for simpler commands, especially if you don't care about the results. If you only need to return a PHP shell script, I will use PassThru (). In general, I use different functions on different occasions, and sometimes they are interchangeable.

It all depends on my mood and the purpose to be achieved. Another question you might ask is "what are their strengths?". If you don't have a clue, or if a project is perfect for using shell commands, but don't know how to use them, I'll give you some insight here. If you are writing an application that provides a variety of backup or file transfer capabilities, you can choose to run the rsync-supported PHP shell script using shell_exec () or one of the other commands provided here. You can write a PHP shell script that contains the necessary rsync commands, and then use PassThru () to execute it based on the user's command or cron job.

For example, if a user has the appropriate permissions in your application (such as Administrator privileges), he wants to send 50 PDF files from one server to another server. Then, the user needs to navigate to the correct location in the application, click Transfer, select the PDF to send, and click Submit. In this process, the form should have a PHP script that uses the return option variable to run the Rsync script through PassThru () so that you know if the problem is occurring, as shown below.

Listing 1. Sample PHP script to run the rsync script via PassThru ()

 
  
  
  1. Php
  2. PassThru (' xfer_rsync.sh ', $returnvalue);
  3. if ($returnvalue! = 0) {
  4. We have a problem!
  5. Add error code here
  6. }else{
  7. We are okay
  8. Redirect to some other page
  9. }
  10. ?>

If your application needs to list processes or files, or data about those processes or files, you can easily do this with one of the commands summarized in this article. For example, a simple grep command can help you find files that match a specific search condition. Use it with the EXEC () command to save the results to an array, which allows you to build an HTML table or form that further allows you to run other commands.

So far, I've talked about user-generated events--the user just presses a button or clicks a link, and PHP runs the script. You can also use standalone PHP scripts with cron or other scheduling programs to achieve some interesting results. For example, if you have a backup script, you can run it through cron, or package it in a PHP script. Why do you do this? It seems to be superfluous, isn't it? This is not the case-you need to consider this, you can run a backup script from exec () or passthru () and then perform some behavior based on the return code. If an error occurs, you can log it to the error log or the database, or send a warning e-mail message. If the script succeeds, you can dump the original output to the database (for example, Rsync has an exhaustive (verbose) mode, which is useful for diagnosing problems later).

Safety

Here's a brief discussion of security: If you accept user input and pass information to the shell, it's a good idea to filter user input. Remove commands and disallowed content that you think are harmful, such as sudo (run as Superuser) or RM (delete). In fact, you might not want users to send open requests, but instead let them choose from a list.

For example, you run a transfer program that accepts a list of files as parameters, and you should list all the files through a series of check boxes. Users can select and deselect files and activate the rsync shell script by clicking Submit. Users cannot enter files themselves or use regular expressions.


http://www.bkjia.com/PHPjc/446466.html www.bkjia.com true http://www.bkjia.com/PHPjc/446466.html techarticle PHP After a long period of development, a lot of users are very familiar with PHP, here I publish a personal understanding, and discuss with you. In most cases, I use the exec () command and the data array ...

  • Related Article

    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.