Mustafa M Jalal
Mustafa M Jalal

Reputation: 421

how to run php file using cron jobs

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?

Upvotes: 18

Views: 59225

Answers (2)

abxstract
abxstract

Reputation: 319

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

Upvotes: 8

wyqydsyq
wyqydsyq

Reputation: 2020

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)

Upvotes: 44

Related Questions