GZ_Dev Posted September 7, 2013 Share Posted September 7, 2013 Hello Experts, I am new to phpfreaks and excited about following it :-) Well I have an issue with sleep function. Whenever I use the sleep function it halts the page (which is supposed to be). But the problem is that it halts the page other than where the sleep is written. For example I have 2 Pages Page a.php ---------- <?php // Some Code sleep(10) // Some Code ?> and Page b.php --------------- <?php // redirect code header("Location: a.php"); ?> When I run a.php through browser it halts (sounds like keep loading) for 10 seconds. (Which is acceptable for me). But When run b.php why does it halt for 10 seconds, and then go to a.php (without halt). What I want is Load b.php, Redirect quickly to a.php where it can halt for 10 seconds (I dont' care) Please Help it is very important Looking forward to having quick and affective responses. Regards Gohar Quote Link to comment https://forums.phpfreaks.com/topic/281952-php-sleep-function/ Share on other sites More sharing options...
requinix Posted September 7, 2013 Share Posted September 7, 2013 It hasn't halted in b.php: your browser got the redirect just fine but it's waiting for the result from a.php to tell it what to do next. For all it knows it's about to get redirected again. It's not showing you the new URL yet because it wants to wait until it knows it's getting content back to display. Quote Link to comment https://forums.phpfreaks.com/topic/281952-php-sleep-function/#findComment-1448584 Share on other sites More sharing options...
GZ_Dev Posted September 7, 2013 Author Share Posted September 7, 2013 Dear Guru :-) Thanks very much for quick response. I am sorry but that was what I meant. I don't want the page b.php should wait for the result of a.php here are the url for the 2 pages http://simplemenu.com/menus/a.php (Have sleep(10) code) http://simplemenu.com/menus/b.php (Just redirects to a.php) I run http://simplemenu.com/menus/b.php and it waits for a.php's result (10 seconds) and then loads a.php Is there anything we can implement using jquery or ajax ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/281952-php-sleep-function/#findComment-1448586 Share on other sites More sharing options...
jazzman1 Posted September 7, 2013 Share Posted September 7, 2013 I don't want the page b.php should wait for the result of a.php Then, pass the commands to the a.php file through a shell terminal, using for instance shell_exec(). Quote Link to comment https://forums.phpfreaks.com/topic/281952-php-sleep-function/#findComment-1448588 Share on other sites More sharing options...
GZ_Dev Posted September 7, 2013 Author Share Posted September 7, 2013 Any easy elaboration on using shell_exec ? Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/281952-php-sleep-function/#findComment-1448589 Share on other sites More sharing options...
jazzman1 Posted September 7, 2013 Share Posted September 7, 2013 Where you stuck? Quote Link to comment https://forums.phpfreaks.com/topic/281952-php-sleep-function/#findComment-1448590 Share on other sites More sharing options...
GZ_Dev Posted September 7, 2013 Author Share Posted September 7, 2013 I actually want to send an email exactly 2 minutes after a form is submitted. I found a solution to use sleep(240) function to freeze the processing for two minutes. I implement the sleep function on response.php page I submit the form from form.php and on successful submit i redirect to response.php page where it waits for 2 minutes before sending an email. Everything works perfect but it waits for 2 minutes when i hit submit form button and then redirects to response.php I want to wait on response.php page Hope this makes sense Quote Link to comment https://forums.phpfreaks.com/topic/281952-php-sleep-function/#findComment-1448592 Share on other sites More sharing options...
jcbones Posted September 7, 2013 Share Posted September 7, 2013 Don't know if it will work the way you want, but you could try to do the sleep function after the redirect. //All of the form processing. //.... ///..... header('Location: http://mysite.com/page2.htm'); sleep(10); //send email //... //... exit(); Quote Link to comment https://forums.phpfreaks.com/topic/281952-php-sleep-function/#findComment-1448593 Share on other sites More sharing options...
requinix Posted September 8, 2013 Share Posted September 8, 2013 I don't want the page b.php should wait for the result of a.phpb.php is not waiting for a.php. Your browser is. b.php finished executing and now your browser is trying to get a.php. The only reason you think b.php is still executing is because your browser has not yet changed the address bar to say "a.php". What you want is what jcbones is basically telling you: send all the headers so that the browser thinks the script is done, then keep script executing. You don't even need to sleep(). IIRC it's something like ignore_user_abort(true); header("Location: a.php"); header("Content-Length: 0"); header("Connection: close"); flush(); // keep executing Quote Link to comment https://forums.phpfreaks.com/topic/281952-php-sleep-function/#findComment-1448771 Share on other sites More sharing options...
jazzman1 Posted September 9, 2013 Share Posted September 9, 2013 Or.....I would suggest another method. When the form has been successfully uploaded to the server, create a new file containing an information of this form (subject, body, mail, e.g) which you want to send to the user. Then, start a cron job to boot this file every minute, when the script is being executed once by cron re-write a file to return a false result, to prevent the script to be executed twice. Quote Link to comment https://forums.phpfreaks.com/topic/281952-php-sleep-function/#findComment-1448785 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.