Page 1 of 1
Detecting Cron job PHP server
Posted: Fri Feb 03, 2012 8:15 am
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!
Re: Detecting Cron job PHP server
Posted: Fri Feb 03, 2012 10:58 am
by Mordred
CLI PHP doesn't set $_SERVER, since there IS NO server. What exactly are you trying to do with it?
Re: Detecting Cron job PHP server
Posted: Fri Feb 03, 2012 9:27 pm
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.
Re: Detecting Cron job PHP server
Posted: Mon Feb 06, 2012 6:29 am
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.
Re: Detecting Cron job PHP server
Posted: Mon Feb 06, 2012 9:01 am
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).