Cron Job Issue

Posted on 2011-10-18
Last Modified: 2012-05-12
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?
Question by:kageboy2003
    LVL 82

    Expert Comment

    by:Dave Baldwin
    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

    LVL 19

    Expert Comment

    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.
    LVL 82

    Expert Comment

    by:Dave Baldwin
    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'.
    LVL 107

    Expert Comment

    by:Ray Paseur
    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

    LVL 19

    Accepted Solution

    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.
    LVL 82

    Assisted Solution

    by:Dave Baldwin
    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now