By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,853 Members | 2,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,853 IT Pros & Developers. It's quick & easy.

Using sessions, and 'please wait' pages

P: 93
Hi, I'm building a site where the user enters some info into a form which submits to a php script. The php script takes a long time to execute (anywhere from 10 seconds to a minute) and when it finally finishes the page that it creates appears to get blocked by any firewall (tried on lots of different browsers/platforms) and you just get a completely blank page. (It's fine when just running on my local machine.)

I'm guessing that the script takes so long to execute that either the connection gets dropped (keep-alive timeout is set to 5, and I can't change it) or the firewall rejects the incoming data because the outgoing request was so long ago that it's forgotten about it.

I'm thinking that the solution is to use a 'please wait' page. So the user submits the form to my script, my script then sends back a session cookie along with a "Location: please_wait.php" header to redirect the user to the 'please wait' page. Whilst they're waiting, my php script does all its processing and sticks the info to be displayed into the $_SESSION array. The please wait page contains a <meta> tag that refreshes (after a few sceonds) to a php script that gets the info from the $_SESSION array and displays it.

And here's the question (at last!!)....
How do I get my script to send the session cookie and the "Location: " header, and THEN do it's processing? At the moment the form submits to a script like this:

[PHP]session_start();
header("Location: please_wait.php");

//do stuff that takes aaaaaages here

$_SESSION['stuff_to_display']=$stuff_that_the_script_made;[/PHP]
But the problem with it is that it doesn't send the header until it has done the processing. How do I get it to send the header and session cookie, and then do the processing? I've tried putting session_commit(); after the header(); line, but it doesn't help.

Any alternative solutions welcome as well.
Thanks
May 31 '07 #1
Share this Question
Share on Google+
7 Replies


Motoma
Expert 2.5K+
P: 3,235
Why not have your "Please wait" page do all of the processing?
Jun 1 '07 #2

P: 93
Surelly that would still have the same problem?... The 'please wait' page would not get sent to the user's browser until it had done the processing. So by the time they got the 'please wait' page they wouldn't need to wait anymore!
Jun 1 '07 #3

cyberking
P: 84
Hi,
Why dont you chacge the timeout settings in your php.ini file to a higher timeout value??
Regards
CyberKing
Jun 1 '07 #4

P: 93
The script is not timing out. Like I said, it works fine running on my local machine, and all the ini settings are the same on my webhost, and I'm not getting a timeout notice.

I've now got as far as getting my script to redirect the user to the 'please_wait' page and THEN do its processing. That seems to work. But the time that my script takes to do its processing can vary a lot. So I need the 'results' page to check to see if the processing has finished yet, and if it has NOT then redirect the user back to the please wait page again to wait for a few more seconds, or if the processing HAS finished then display the information. So the user just gets bounced back to the please wait page as many times as is necessary until the processing is done.

I've created some basic scripts to test this idea....

My page with the form on it:
[HTML]<form action="do_stuff.php" method="post">
<input type="text" name="textbox">
<input type="submit" value="go"></form>[/HTML]

'do_stuff.php':
[PHP]//redirect the user to the please wait page:
header("Location: please_wait.php");
session_start(); //that sets the session cookie
//then lots of whitespace to make the browser display the page (see the 'flush' page of the php.net manual)
?>




<?php
//send all of the output so far, so that the user goes to the please wait page
while (@ob_end_flush());
while (@flush());

//now do some processing that takes a long time!
$time=time()+15;
while(time()<$time)
{
//do nothing
}
//and when that's done some data will be created:
$_SESSION['textbox']=$_POST['textbox'];[/PHP]


And then the please wait page will redirect itself after 2 seconds;
'please_wait.php':
[HTML]<html><head>
<meta http-equiv="Refresh" content="2; url=results.php">
</head><body>
Please wait while we do stuff
</body></html>
Jun 1 '07 #5

P: 93
Dammit!! My posts never post properly. Half of that is missing! I think there's some kind of crap that happens when I copy and paste from dreamweaver to Firefox. It thinks that the end of the pasted text is the end of the post. Anyway, now I have to rewrite half of that again!...

So....

'results.php' is like this:
[PHP]session_start(); //get the user's session id so that $_SESSION works

if(!isset($_SESSION['textbox'])) //if the data is NOT yet ready
{
header("Location: please_wait.php"); //redirect them again
//more whitespace so that the browser will stop waiting for more stuff
?>





<?php
//send everything so far
while (@ob_end_flush());
while (@flush());
}
else //if the data IS ready
{
print "Your results are ready<br><br>";
print "You entered ". $_SESSION['textbox'];
}[/PHP]

The problem is that 'results.php' never redirects you back to the please wait page, even if the data isn't ready...it just waits until the data is ready and then sends it. I.e. php seems to know that $_SESSION is still in use in 'do_stuff.php' so instead of the isset() returning false, it waits until 'do_stuff.php' is finished, and then it returns true. Which is totally useless!

I need another way of checking to see if 'do_stuff.php' has finished processing.
Jun 1 '07 #6

P: 93
And 2 minutes after I spent all that time typing...I've done it!!!

No 2 scripts can write to the same session at once. So 'do_stuff.php' needs to be like this:[php]
header("Location: please_wait.php");
session_start();
//then whitespace
?>


<?php
while (@ob_end_flush());
while (@flush());

session_write_close();
//so this script is now done with the current session so that other scripts can access $_SESSION

//now do some processing that takes a long time!
$time=time()+15;
while(time()<$time)
{
//during this time any other script can access $_SESSION because this script is NOT using it (so 'results.php' can check isset($_SESSION)
}

//and now that we're done processing..
session_start(); //open the session up again
$_SESSION['textbox']=$_POST['textbox']; //stick the data in
session_write_close(); //and close it again (actually not necessary cos the session will be close when this script ends anyway)[/PHP]

I hope this saves someone else some time!!!
Jun 1 '07 #7

Motoma
Expert 2.5K+
P: 3,235
Glad you figured out your problem. Sorry we did not fully understand your issue. thanks for posting the solution for the rest of the community!
Jun 1 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.