note 112843 added to features.connection-handling
| From: | mheumann at comciencia dot cl | Date: | Mon, 29 Jul 2013 14:38:55 +0000 |
| Subject: | note 112843 added to features.connection-handling | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195807@lists.php.net to get a copy of this message | ||
I had a lot of problems getting a redirect to work, after which my script was intended to keep
working in the background. The redirect to another page of my site simply would only work once the
original page had finished processing.
I finally found out what was wrong:
The session only gets closed by PHP at the very end of the script, and since access to the session
data is locked to prevent more than one page writing to it simultaneously, the new page cannot load
until the original processing has finished.
Solution:
Close the session manually when redirecting using session_write_close():
<?php
ignore_user_abort(true);
set_time_limit(0);
$strURL = "PUT YOUR REDIRCT HERE";
header("Location: $strURL", true);
header("Connection: close", true);
header("Content-Encoding: none\r\n");
header("Content-Length: 0", true);
flush();
ob_flush();
session_write_close();
// Continue processing...
sleep(100);
exit;
?>
But careful:
Make sure that your script doesn't write to the session after session_write_close(), i.e. in
your background processing code. That won't work. Also avoid reading, remember, the next
script may already have modified the data.
So try to read out the data you need prior to redirecting.
----
Server IP: 69.147.83.199
Probable Submitter: 201.223.235.43
----
Manual Page -- http://php.net/manual/en/features.connection-handling.php
Edit -- https://master.php.net/note/edit/112843
Del: integrated -- https://master.php.net/note/delete/112843/integrated
Del: useless -- https://master.php.net/note/delete/112843/useless
Del: bad code -- https://master.php.net/note/delete/112843/bad+code
Del: spam -- https://master.php.net/note/delete/112843/spam
Del: non-english -- https://master.php.net/note/delete/112843/non-english
Del: in docs -- https://master.php.net/note/delete/112843/in+docs
Del: other reasons-- https://master.php.net/note/delete/112843
Reject -- https://master.php.net/note/reject/112843
Search -- https://master.php.net/manage/user-notes.php