Detecting Cron job PHP server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Detecting Cron job PHP server

Post by Eric! »

Anyone know a clean way to determine what server is running when executing a PHP script via a cron job? In other words there's no $_SERVER information.

Thanks!
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Detecting Cron job PHP server

Post by Mordred »

CLI PHP doesn't set $_SERVER, since there IS NO server. What exactly are you trying to do with it?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Detecting Cron job PHP server

Post by Eric! »

I have a few cron jobs that need to run a little differently depending on what server/system it is installed on. So I'm looking for a way to see what server is running the php script so it will behave properly.

Some servers don't have shell access. I guess I could scrape the server's IP off whatismyip.org, but it would be slow. I think most of the servers are running at least php 5.3 so I could try gethostname()....

Just seems like a bit of a hack.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Detecting Cron job PHP server

Post by Mordred »

1. cronjob.php with a .htaccess to allow hitting it only from 127.0.0.1
2. Cronjob that does lynx -dump http://server.com/cronjob.php
3. Voila, $_SERVER in cronjob.php :)
(4. Don't forget to verify that you can't hit it from 'abroad' and just in case make sure it doesn't do anything stupid, since theoretically someone from the same shared hosting could hit the same URL)

----

Or, of course, you can just customize each installation to know in a static include file whatever you need to know about it.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Detecting Cron job PHP server

Post by Eric! »

Mordred wrote:(4. Don't forget to verify that you can't hit it from 'abroad' and just in case make sure it doesn't do anything stupid, since theoretically someone from the same shared hosting could hit the same URL)
Nice solution. I didn't even think of trying that. Would allowing 127.0.0.1 allow another account on that server to run the cron job? I'm not sure how to test that without having two accounts. (not that running these cron job remotely will do anything interesting anyway...blank screens don't reveal too much).

I'm too lazy to do anything statically if I don't have too. Besides I'll probably forget to tweak something static and break the site(s).
Post Reply