简体   繁体   中英

Why does putenv() work but export doesn't?

In php, running:

echo shell_exec("export http_proxy=http://myproxy.com:8080 2>&1");
echo shell_exec("env | grep proxy");

Results in a blank output. It seems that the environment variable is not set, but the export command also does not give any errors. However, this works:

putenv("http_proxy=http://myproxy.com:8080");
echo shell_exec("env | grep proxy");

Result:

http_proxy=http://myproxy.com:8080

Why is export not working? Does the environment variable get unset after export finishes? Or is this some kind of security setting? OS is CentOS 7.

Each shell_exec call runs in its own shell child process. Changes to the environment in one invocation do not persistent into the other. (A process cannot modify the environment of its parent or siblings).

putenv on the other hand, modifies the current (PHP) process's environment, which is then inherited by all shell_exec child processes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM