GoDaddy Cron Job PHP include files

As a follow-up to figuring out how to get Cron Jobs to actually run in GoDaddy with a PHP script, I ran into another error:

I have a PHP Cron job that worked on HostGator, but it didn’t work on GoDaddy.  After I figured out the correct command to use, I noticed that it wasn’t actually connecting to the database.

At first, I didn’t notice this because I typically “include” files instead of “require” them.  I read somewhere that it takes up less processing power; so I figure once I know the location of the file, I can just “include” it (instead of “require” it).  Unfortunately, changing hosts caused an unforeseen inssue!  So that was the first mistake…

Going to the URL (in the browser), everything worked (i.e. it would connect to the database).  However that when running the Cron job, it didn’t work.  I found this out by printing out (in the email) the current working directory ( cwd(); ).  On the web server, it was as expected:

 /home/<username>/public_html/<sitename>/file.php

However the Cron job runs it from the root!

/home/<username>

Usually I just reference a configuration file with “include(“../path/relative/to/here”).  But that didn’t work!  The solution was to explicitly state the entire path of the file you want to include.

However then I ran into another problem:

I typically have code that detects the URL to determine if I’m in a sandbox or production environment.  GoDaddy’s Cron job, the PHP function $_SERVER[‘SCRIPT_FILENAME’] doesn’t return anything, so you can’t use that to determine which environment you’re in.  For now, I think I’ll have to just have 2 version of the file and comment/uncomment out the appropriate database connection to serve the correct data source.

By the way: if you thought this blog post was helpful, check out the book I wrote on PHP, geared toward intermediate web developers.

No
Comments

Comments are now closed.