As we all know, in computer science, the shell resembles a DOS command.com. It receives the user command and then invokes the appropriate application. And it's also a programming language.
As a command language, it interactively interprets and executes user-entered commands or automatically interprets and executes a predetermined set of commands; As a programming language, it defines variables and parameters and provides many control structures in higher-order languages, including loops and branches.
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 ()
The code is as follows:
<?php passthru (' xfer_rsync.sh ', $returnvalue); if ($returnvalue! = 0) {//we Have a problem! Add error code here}else{//we is okay//redirect to some other page};
If your application needs to list processes or files, or data about those processes or files, you can do this easily using 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 discussed user-generated events-the user simply presses the button or clicks the 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 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).
Security
We'll briefly discuss security here: 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.
This article introduces the use of PHP shell scripts from two aspects, hoping to bring you help.