1
votes

Short Story: I'm looking for a work around to PHP $_SERVER[] variables not being available (only) in a case where a PHP script file is being run from a Windows Task Scheduler scheduled event.

I have two Windows 2008 R2 Servers with PHP version 7.1 running on both servers. I've been calling server #1 'Production' and server #2 'Development'. Each server has it's own MySQL db.

On the pages I've been writing, for MySQL db read/writes, I've been using $_SERVER['SERVER_NAME'], to identify what server is running the script (if the page is running on the development server, use the IP address of the MySQL db on the development server. If the current server is the production server, use the production database IP).

That works perfectly fine for me. The exception is when I use the Windows Task Scheduler to schedule an automated task. In this case, I'm creating a single stand alone PHP script file to send an email with some information from that database.

PHP script file to send an email with some information from the db results in:

  • Running the page in a browser results in $_SERVER['SERVER_NAME'] being defined (works good).
  • If Windows Task scheduler runs the PHP page this results in $_SERVER['SERVER_NAME'] not being defined

Would anyone know how I could make the $_SERVER[] variables available to a .php file being run by a Windows Scheduled Taks (where the task scheduler is of course pointing towards the PHP.exe with a referee to this script .php file)? Or, is there another way besides the $_SERVER[] variable to detect the name of the server the script is being run on?

2
That has little to do with the task scheduler. Scripts running in the CLI will never have any HTTP server environment variables available either. Use a wrapper script or something. - mario
This is what i do : my scheduled tasks are small php scripts who use curl to issue http request to the server, for specific api servlets designed for each task. - YvesLeBorg
Nice. Thank you. - Reno
Did you give up? - AbraCadaver

2 Answers

0
votes

You can use the environment variables:

echo $_ENV['COMPUTERNAME'];

Just make sure that the variables_order is set correctly in php.ini to include E such as EGPCS.

Or:

echo getenv('COMPUTERNAME');
0
votes

here is my centos solution. will be a little different from the windows solution. but in the same spirit. basically i just tag the cron job with the subdomain (vs server name). Then I check if this is being run on localhost or from apache and substitute $cron_host for the server variable as needed.

Cron:

#crontab -u apache -e
30  3 * * * php /var/www/vhosts/the/script.php -h=staging.domain.com -t=11:30

cron_file.php:

// get hostname from varg
$opts = getopt("h:t:");
$cron_host = $opts['h']; // which host to load from creds file
$time_max  = $opts['t']; // time parameter for database call

creds_file.php:

$localhost = array( '127.0.0.1', '::1' );
$is_localhost = ( !is_array($_SERVER) || !isset($_SERVER['REMOTE_ADDR']) || in_array( $_SERVER['REMOTE_ADDR'], $localhost) ) ? true : false;
$cron_host    = (isset($cron_host)) ? $cron_host : false;
$host_name    = ($is_localhost) ? $cron_host : ((isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']);