Jump to content

PHP Sleep Function


GZ_Dev

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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();
Link to comment
Share on other sites

I don't want the page b.php should wait for the result of a.php

b.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
Link to comment
Share on other sites

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. 

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.