从命令行或system()运行时,不同的$ _SERVER输出

时间:2018-10-01 13:20:11

标签: php command-line-interface

如果有以下非常简单的脚本:

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

1 个答案:

答案 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().

相关问题