如果有以下非常简单的脚本:
foo.php:
<?php
system("php -c /etc/php7/cli/php.ini /some-path/exec.php");
?>
exec.php:
<?php
print_r($_SERVER);
?>
从命令行运行exec.php
,我得到$_SERVER
的预期输出:
...
[LESSKEY] => /etc/lesskey.bin
[NNTPSERVER] => news
[MANPATH] => /usr/share/man:/usr/local/man:/usr/local/share/man
[XDG_SESSION_ID] => 2
[HOSTNAME] => AAEB-DEV203LD
[XKEYSYMDB] => /usr/X11R6/lib/X11/XKeysymDB
[HOST] => AAEB-DEV203LD
[TERM] => linux
[SHELL] => /bin/bash
...
在浏览器选项卡中调用的exec.php
脚本中,由system
函数执行foo.php
时,$_SERVER
的输出完全不同,看起来或多或少与Environment
中的phpinfo()
部分完全一样:
...
[APACHE_CONF_INCLUDE_FILES] =>
[mpm_found] => true
[APACHE_CONF_INCLUDE_DIRS] =>
[SYSCONFIG_FILE] => /etc/sysconfig/apache2
[APACHE_START_TIMEOUT] => 2
[HTTPD_MODULE_IDS] => actions_module alias_module ...
[APACHE_SERVERNAME] =>
...
问题出在哪里:
在$_SERVER
输出中-通过system()
函数运行-缺少许多我们需要的信息。
我在Internet上找不到任何东西,可以提示我为什么输出差异如此大。
OS:SLES 12.3
PHP:7.2.10
Apache:2.4 MPM
我的问题:
在Apache会话中在命令行和system()
下运行时,为什么输出差异如此之大?当从命令中调用system()
时,我可以为exec.php
函数获得相同的输出吗?符合php -c /etc/php7/cli/php.ini /some-path/exec.php
?
答案 0 :(得分:1)
Good. I could sort it out how it works, it wasn't clear to me.
Running php
from command line PHP sets into $_SERVER
all exported environment variables. Therefore $_SERVER
has the entry HOSTNAME
.
Running exec()
from a script which is executed in a browser tab PHP sets $_SERVER
with the content of Environment
section as displayed by phpinfo()
.
When I want to have set the entry HOSTNAME
in $_SERVER
I have to call putenv("HOSTNAME=value");
before exec()
.