Cron Job Issue

I had a cron job running on a linux server with:
Debian 5.0 - Plesk 9.3 - RAID1 - 64bit

I duplicated the application to a new linux server using:
Parallels Plesk Panel v10.3.1_build1012110812.15 os_CentOS 5

The cron job ran perfect on the first server.  Now, I am getting PHP Notice: Undefined Index for HTTP_HOST and some other Server variables......

This is an email cron that picks up scheduled email jobs out of a table and mails out newsletters.  I am using a PHP class that relies on the server variables.

Can anyone explain why on one linux server it works on on another it dosn't?
Who is Participating?
Well, how are you running this cron?   If you're simply calling "php /path/to/page.php" via cron, of course all the SERVER variables will be empty, because you're not using the Apache server.  You can't get HTTP_HOST and REQUEST_URI when there was no HTTP GET.

If the web page relies on knowing how and where its called, you might consider instead setting a cron which uses wget or curl to go through actual http to retrieve the page as a mock browser client, and then the SERVER variables will be populated.
Dave BaldwinFixer of ProblemsCommented:
There are several reasons.  The first is that the server admin can change what server variables are reported.  Second is that different version of the different web servers report different variables anyway.  Third is that the 'php.ini' used by command line programs is often different than the one used on the web server.

You can run the PHP page below on the two servers and on the command line to see what server variables are reported.  That will tell you what is available in each situation.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<title>PHP Server Variables</title>
<body><h1>PHP Server Variables</h1>

foreach($_SERVER as $key => $value) {
    echo "<b>$key :</b> $value<br />\n";


Open in new window

It could be because on the 2nd server, register_globals = Off in php.ini (where it was On on the first server.)

Try using $_SERVER['HTTP_HOST'] to access it, and see if your luck changes.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Dave BaldwinFixer of ProblemsCommented:
FYI, I just ran my program above from the command line on IIS and on Apache.  None of the 'normal' $_SERVER variables show up on either one.  This is actually not surprising because it is not running thru the web server when invoked from the command line.  What does show up are a lot of 'environment variables'.
Ray PaseurCommented:
If you want to see what PHP knows about in the CRON job, you can install this as a cron job and trigger it.  Untested code but valid in principle.  I have dealt with what you are facing and I recall that after I could see the variables things got easier very fast.  Best of luck, ~Ray
<?php // RAY_cron_display.php
ini_set('display_errors', TRUE);
$msg = ob_get_contents();
mail('', 'Cron Output', $msg);

Open in new window

Dave BaldwinFixer of ProblemsCommented:
I agree with @xterm.  This little program below will run the program above thru the web server where it will see the $_SERVER variables.  If your page will run correctly on the web server thru a browser, you can use this technique to run it from the command by 'calling' it to run on the web server.  Using 'http' method in file_get_contents() turns it into a browser-like request to the page on the web server.
// Report all PHP errors

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

$pgcntent = file_get_contents("");
echo $pgcntent;

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.