Agargara
Agargara

Reputation: 932

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.

Upvotes: 1

Views: 706

Answers (1)

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137448

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.

Upvotes: 3

Related Questions