16

I have a E-mail schedule runs everyday on php page using cron jobs. The php code workds fine when i run the page using a link.

Now when I run the php script using cron jobs, it also works fine but when I put some query the cron jobs won't understand the link.

for example: http://www.wetube.org/cron.php?id=01001 so now if I try to run this everyday using cron job it's doesn't work.

But if we just erase the query it works fine. Do you guys know any code which makes this link work in cron job?

Kate Gregory
  • 18,565
  • 8
  • 53
  • 85
Mustafa M Jalal
  • 271
  • 1
  • 4
  • 16

2 Answers2

41

Cron runs commands as they would be ran via the shell, so running PHP would use local paths.

You need to use a command like:

php /home/USER/public_html/cron.php

Or if including the query string is necessary, use cURL instead (if it's installed):

curl http://www.wetube.org/cron.php?id=01001

You might want to look at not exposing your cron scripts to the internet - move them to outside your web directory because if someone finds it they can constantly reload it to spam your cron scripts (i.e. sending lots of emails)

wyqydsyq
  • 1,932
  • 1
  • 19
  • 27
  • 4
    If you have to run the script via HTTP (which is generally a bad idea in the first place) and your server has a static (and dedicated) IP address you can limit access to only come from your server by enclosing your cron script in a conditional checking that the `$_SERVER['REMOTE_HOST']` matches your server's IP Address. This isn't foolproof though and your safest bet is to simply have the cron script outside your web directory. – wyqydsyq Apr 11 '12 at 00:00
7

I would add hash like

curl http://www.wetube.org/cron.php?id=01001&hash=cm349ucKuc023b2ynGyv23ycr23

and in php file

if(isset($_GET['hash']) && $_GET['hash']=='cm349ucKuc023b2ynGyv23ycr23'){
....
stuff to do
....
}

*you can even add specific time/date check when it should be run.
*you can check IP
*generate sha512 (I would recommend) hashes in both cron and php file with the same salt and maybe even time and then check if they are the same - it would be impossible for a hacker to recreate it - except if he somehow gets your original hash setup

abxstract
  • 189
  • 2
  • 11