Welcome to WebmasterWorld Guest from 35.175.120.174

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Detecting cron job

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

New User

10+ Year Member

joined:Jan 6, 2009
posts: 20
votes: 0


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
2:18 am on Nov 28, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2003
posts:960
votes: 34


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?
3:47 am on Nov 28, 2010 (gmt 0)

Full Member

10+ Year Member

joined:June 1, 2007
posts:201
votes: 0


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
1:56 pm on Nov 28, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Dec 13, 2009
posts:945
votes: 0


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.
1:59 pm on Nov 28, 2010 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:5046
votes: 60


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.
10:31 pm on Nov 28, 2010 (gmt 0)

New User

10+ Year Member

joined:Jan 6, 2009
posts: 20
votes: 0


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