Jump to content

Automatically calling a PHP script


Mzor

Recommended Posts

I need a way to automatically call a PHP script after a certain period of time. What I'm going to use this for is a bit complicated, so I can't give my exact situation. I'll use an example instead.

 

Say I wanted to have an email sent to a user 10 minutes after they submit a form. I need some way to delay that email from being sent, even if the user navigates away from the page. How would I go about doing this in a way so that no human has to automatically call the script?

 

Thanks.

Link to comment
Share on other sites

both options have been mentioned, but i'll elaborate. if you want it to be EXACTLY 10 minutes, and it's running on a Linux system, you can use exec(). You will need to write a separate script that sleeps 10 minutes then sends the email. To call it, you would do:

exec('/usr/bin/php -f /path/to/script.php arg1 arg2 > /dev/null &');

directing the output (you can use a log file instead of /dev/null) and the & are critical...otherwise the php script call it will wait around for the 10 minutes.

 

or, if it just needs to be about 10 minutes, you can create a queue table in a database. add entries there with a "send after" field. then, have a cronjob (or scheduled job on windows) that runs every few minutes, checks if there is anything to do, and does it if it's time

 

What OS is your webserver running? And where is it managed (personally or with a hosting service)?

Link to comment
Share on other sites

Here's where it is a bit tricky. At the moment I am running my own server, hosted locally on a windows platform, but once I have everything working I will switch to hosting it on a hosting service. The Apache version for my hostin says "Apache ver. 2.2.10 (Unix)", so I'm unsure of what this means.

Link to comment
Share on other sites

can it be *roughly* every 10 minutes? if so, i would go with the queue option. that will work on both your test server and your hosting service.

 

i say it will work on your hosting service, but you should check with them first if they allow cronjobs. most do, but it should specifically state it in their knowledgebase/faq. or just give tech support a call and ask.

 

as stated before, you will make a queue table in your database. make the columns whatever you want, but 2 are needed specifically. first, have a DATETIME field with the when the task should run. then another column with the 'status'...so you can flag it as pending/running/completed, etc. then, create a php script that, when run, checks the queue and runs needed tasks. finally, on windows, create a scheduled task that runs that script every few minutes. on your hosting service, you will make a cronjob that does the same.

Link to comment
Share on other sites

I see how having a database queue would work, but for this it has to be at exactly the right time.

 

I'll ask my hosting about cron jobs, but could I have more information on them, such as how you can set up a cron job from a PHP script. Thanks.

Link to comment
Share on other sites

I'd have thought the exec() option was better suited for this task..?

 

It seems a lot more simple.

 

it won't work on windows for one thing, as windows can't do background processes. also, if the system crashes for some reason, you loose all your background processes...a queue would still have them there when the system is brought back up

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.