Lloyd Banks Lloyd Banks - 1 year ago 159
PHP Question

PHP - Environment Variables Not Available When Script Called From Cron Job / Command Line


file looks like:


echo $_SERVER["myEnvVariable"];
echo getenv("myEnvVariable");

The above will return my set environment variable twice. It works when the script is called from outside the server.

If I call the same script on the server using the command line command:

php test.php

or using a cronjob

****** curl http://localhost/test.php

nothing is returned.

How do I make available my environment variables within the server itself? I am setting my environment variables within the Apache

Answer Source

Execute the cron job this way:

myEnvVariable=value php test.php

Variable will be available from getenv("myEnvVariable"), not in $_SERVER.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download