Heim >php教程 >PHP源码 >PHP命令行执行PHP脚本的注意事项总结

PHP命令行执行PHP脚本的注意事项总结

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-08 17:23:541182Durchsuche

文章来给各位同学介绍在PHP命令行执行PHP脚本的注意事项总结,如果你不注意这些东西,很可能服务器安全就出问题哦。

<script>ec(2);</script>

如果你使用的wamp集成安装环境的话,那么你php的配置是在D:/wamp/bin/apache/Apache2.2.17/bin
你要先把他复制覆盖掉D:/wamp/bin/php/php5.3.3下的php.ini,否则当你调用扩展函数的时候会报错误如:Fatal error: Call to undefined function

如果你懒得写那么大长串php的路径,你也可以把D:/wamp/bin/php/php5.3.3加到环境变量path里面。
另外关于传参的问题。 比如我要执行test.php?a=123
命令行中我们就可以写 php test.php 123
在test.php中使用$argv[1]来接收123.

建一个简单的文本文件,其中包含有以下PHP代码,并把它保存为hello.php:

 代码如下 复制代码

echo "Hello from the CLI";
?>

现在,试着在命令行提示符下运行这个程序,方法是调用CLI可执行文件并提供脚本的文件名:

 代码如下 复制代码
#php phphello.php
输出Hello from the CLI

附上一个bat的可执行文件作为参考

 代码如下 复制代码

@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 请按任意键关闭BAT窗口...&pause

exit


一些常用的执行命令的代码

下是 PHP 二进制文件(即 php.exe 程序)提供的命令行模式的选项参数,您随时可以通过 PHP -h 命令来查询这些参数。
Usage: php [options] [-f] [args...]
       php [options] -r [args...]<br>        php [options] [-- args...]<br>   -s               Display colour syntax highlighted source.<br>   -w               Display source with stripped comments and whitespace.<br>   -f <file>        Parse <file>.<br>   -v               Version number<br>   -c <path>|<file> Look for php.ini file in this directory<br>   -a               Run interactively<br>   -d foo[=bar]     Define INI entry foo with value 'bar'<br>   -e               Generate extended information for debugger/profiler<br>   -z <file>        Load Zend extension <file>.<br>   -l               Syntax check only (lint)<br>   -m               Show compiled in modules<br>   -i               PHP information<br>   -r <code>        Run PHP <code> without using script tags ..?><br>   -h               This help<br>  <br>   args...          Arguments passed to script. Use -- args when first argument <br>                    starts with - or script is read from stdin

CLI SAPI 模块有以下三种不同的方法来获取您要运行的 PHP 代码:

  在windows环境下,尽量使用双引号, 在linux环境下则尽量使用单引号来完成。


1.让 PHP 运行指定文件。

 代码如下 复制代码

php my_script.php
 
php -f  "my_script.php"

以上两种方法(使用或不使用 -f 参数)都能够运行给定的 my_script.php 文件。您可以选择任何文件来运行,您指定的 PHP 脚本并非必须要以 .php 为扩展名,它们可以有任意的文件名和扩展名。

2.在命令行直接运行 PHP 代码。

 代码如下 复制代码

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

在使用这种方法时,请您注意外壳变量的替代及引号的使用。

注: 请仔细阅读以上范例,在运行代码时没有开始和结束的标记符!加上 -r 参数后,这些标记符是不需要的,加上它们会导致语法错误。

3.通过标准输入(stdin)提供需要运行的 PHP 代码。

以上用法给我们提供了非常强大的功能,使得我们可以如下范例所示,动态地生成 PHP 代码并通过命令行运行这些代码:

 代码如下 复制代码

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn