Mzor Posted March 16, 2009 Share Posted March 16, 2009 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. Quote Link to comment Share on other sites More sharing options...
jackpf Posted March 16, 2009 Share Posted March 16, 2009 As a guess, maybe you could do something like call a script with exec() or system(), and then on the script have a sleep command? ..maybe? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted March 16, 2009 Share Posted March 16, 2009 you could run a cron job Quote Link to comment Share on other sites More sharing options...
Mzor Posted March 16, 2009 Author Share Posted March 16, 2009 you could run a cron job Sorry, erm... cron job? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted March 16, 2009 Share Posted March 16, 2009 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)? Quote Link to comment Share on other sites More sharing options...
Mzor Posted March 16, 2009 Author Share Posted March 16, 2009 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. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted March 16, 2009 Share Posted March 16, 2009 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. Quote Link to comment Share on other sites More sharing options...
Mzor Posted March 16, 2009 Author Share Posted March 16, 2009 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. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted March 16, 2009 Share Posted March 16, 2009 it's an OS level thing. you would edit them by logging into your server via SSH and running command line stuff or using some GUI control panel provided by your hosting service. Quote Link to comment Share on other sites More sharing options...
jackpf Posted March 16, 2009 Share Posted March 16, 2009 I'd have thought the exec() option was better suited for this task..? It seems a lot more simple. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted March 16, 2009 Share Posted March 16, 2009 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 Quote Link to comment Share on other sites More sharing options...
jackpf Posted March 16, 2009 Share Posted March 16, 2009 That's a good point...I didn't think of that. Yeah, hard decision really. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.