Lloyd Banks Lloyd Banks - 2 months ago 22
PHP Question

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

My

test.php
file looks like:

<?php

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
httpd.conf
file.

Answer

Execute the cron job this way:

myEnvVariable=value php test.php

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