PHP Command Line PHP script execution considerations-PHP source code

Source: Internet
Author: User
This article will introduce you to the considerations for executing the PHP script on the PHP Command Line. If you do not pay attention to these things, it is very likely that there will be problems with server security. This article will introduce you to the considerations for executing the PHP script on the PHP Command Line. If you do not pay attention to these things, it is very likely that there will be problems with server security.

Script ec (2); script

If you use the wamp integrated installation environment, Your php configuration is at D:/wamp/bin/apache/Apache2.2.17/bin.
You need to copy and overwrite php under D:/wamp/bin/php/php5.3.3. ini. Otherwise, an error such as Fatal error: Call to undefined function will be reported when you Call the extended function.

If you are too lazy to write such a long string of php paths, you can also add D:/wamp/bin/php/php5.3.3 to the environment variable path.
In addition, we have questions about parameter passing. For example, I want to execute test. php? A = 123
In the command line, we can write php test. php 123
Use $ argv [1] In test. php to receive 123.

Create a simple text file that contains the following PHP code and save it as hello. php:

The Code is as follows:

Echo "Hello from the CLI ";
?>

Now, try to run this program at the command line prompt by calling the CLI executable file and providing the script file name:

The Code is as follows:
# Php phphello. php
Output Hello from the CLI

Attach a bat executable file for reference

The Code is as follows:

@ Echo off

Php D:/wamp/www/taobao/items. php 158345687

Php D:/wamp/www/taobao/refunds_up.php 158345687

Php D:/wamp/www/taobao/trade. php 158345687

Echo. & echo press any key to close the BAT window... & pause

Exit


Code for executing Common commands

The following are the command line mode option parameters provided by the PHP binary file (php.exe Program). You can use the PHP-h command to query these parameters at any time.
Usage: php [options] [-f] [Args...]
Php [options]-r [args...]
php [options] [-- args...]
-s Display colour syntax highlighted source.
-w Display source with stripped comments and whitespace.
-f Parse .
-v Version number
-c | Look for php.ini file in this directory
-a Run interactively
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-z Load Zend extension .
-l Syntax check only (lint)
-m Show compiled in modules
-i PHP information
-r Run PHP without using script tags
-h This help

args... Arguments passed to script. Use -- args when first argument
starts with - or script is read from stdin

The cli sapi module has the following three methods to obtain the PHP code you want to run:

In windows, use double quotation marks as much as possible. In linux, use single quotation marks as much as possible.


1. Run the specified file in PHP.

The Code is as follows:

Php my_script.php

Php-f "my_script.php"

The above two methods (using or without the-f parameter) can run the given my_script.php file. You can select any file to run the script. The PHP script you specified does not have to use. php as the extension. They can have any file name and extension.

2. Run the PHP Code directly on the command line.

The Code is as follows:

Php-r "print_r (get_defined_constants ());"

When using this method, pay attention to the substitution of shell variables and the use of quotation marks.

Note: Read the preceding example carefully. There is no start or end mark when running the code! After the-r parameter is added, these tokens are not required, and adding them will lead to syntax errors.

3. Provide the PHP code to be run through standard input (stdin.

The above usage provides us with very powerful functions, so that we can dynamically generate PHP code and run the Code through the command line as follows:

The Code is as follows:

$ Some_application | some_filter | php | sort-u> final_output.txt

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.