Forum Moderators: coopster

Message Too Old, No Replies

Detecting cron job

         

mightymouse3062

12:30 am on Nov 28, 2010 (gmt 0)

10+ Year Member



Good Evening,
I am working on creating a script and I want to detect if the request is coming from a browser or the server. The server will be using a cron job.

I looked online and found this discussion, [stackoverflow.com...] and I was looking at the solution of finding the root of the process tree. How would I do that in php?

Thanks,
Mike

rainborick

2:18 am on Nov 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you do something simpler like creating a copy of the PHP script using a different file name, and have browsers use the main copy and the cron job call the second copy?

bkeep

3:47 am on Nov 28, 2010 (gmt 0)

10+ Year Member



Another down and dirty approach is setup an access key and use one with the cron and one from the regular browser. or if a certain variable was set ie ?source=cron and matches then log it as a server run else it must be a browser? or vice-versa

Readie

1:56 pm on Nov 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put the file to be cronned somewhere visitors can't reach it (i.e. above public_html or www_root or whatever it's called on your server) and create a visitor accesible file that runs a require() function on the cron script.

You can then set a variable in the parent file, and check for this variable in the actual cron script.

brotherhood of LAN

1:59 pm on Nov 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I used whether $_SERVER['SERVER_NAME'] is set, it won't be when initiating a cron, and will be when accessing your script via HTTP.

mightymouse3062

10:31 pm on Nov 28, 2010 (gmt 0)

10+ Year Member



would a combination of using isset($_SERVER['SERVER_NAME']) and if($_SERVER['SERVER_ADDR'] == $_SERVER['REMOTE_ADDR']) {
Request coming from server
} else {
Request not coming from server
}

The one site that I want to run the script cannot be done with a cron job because Yahoo Hosting does not support it, so I am going to use another server with the command lynx -dump [fileurl]. Since I am using lynx -dump it will have the server name variable set and the server address and remote address will not be the same so that wont work.

I printed out $_SERVER and I saw that HTTP_CONNECTION, HTTP_KEEP_ALIVE, and HTTP_COOKIE were not set in the lynx -dump when they were set in the browser. Could something along those lines work?

Thanks,
Mike