How to cause a redirect to occur before php script finishes?
If you want to run script continuously and still want to display some output then, why don't you use an ajax request to server which can queue mails and still let user continue browsing that page.
If you don't want to use this; instead you could use,
the script runs background even if the user will be redirected to show_usermessage.php
page
<?PHP
//Redirect to another file that shows that mail queued
header("Location: show_usermessage.php");
//Erase the output buffer
ob_end_clean();
//Tell the browser that the connection's closed
header("Connection: close");
//Ignore the user's abort (which we caused with the redirect).
ignore_user_abort(true);
//Extend time limit to 30 minutes
set_time_limit(1800);
//Extend memory limit to 10MB
ini_set("memory_limit","10M");
//Start output buffering again
ob_start();
//Tell the browser we're serious... there's really
//nothing else to receive from this page.
header("Content-Length: 0");
//Send the output buffer and turn output buffering off.
ob_end_flush();
flush();
//Close the session.
session_write_close();
//Do some of your work, like the queue can be ran here,
//.......
//.......
?>
PHP Redirect User and Continue Process Script
The other answers seem to be missing the point, why does the browser wait for the whole response to finish before redirecting.
I'm not positive about it, but is it a given that browsers will redirect immediately upon receiving the Location header? Or is it done when the connection terminates? Use Firebug to see what the network connection is doing and see if it prints those headers before the 10 seconds is up, or if it appears after 10 seconds.
In the Ruby on Rails world, the preferred solution is to send the job to a "delayed job" or "resqueue" server to run; this avoids trying to run long processes in a web server that needs to be available to handle web requests.
Redirect to specified URL on PHP script completion?
<?
ob_start(); // ensures anything dumped out will be caught
// do stuff here
$url = 'http://example.com/thankyou.php'; // this can be set based on whatever
// clear out the output buffer
while (ob_get_status())
{
ob_end_clean();
}
// no redirect
header( "Location: $url" );
?>
Redirect page after process complete in PHP
Your code should be fine, and should complete the process before redirecting.
PHP runs in a single thread, so the next statement won't be executed until the previous one completes. This means that your header()
line won't execute until fwrite()
has finished writing the file.
In fact, setting a Location
header doesn't even mean the PHP script stops. You can test this by creating a dummy file and running unlink()
on it after issuing the Location
header. The file will still be deleted. All that Location
does is sends a new location to your browser. Your browser then retrieves the new page before the old one is done running.
PHP redirection in between
In case you still have to echo before your point where you may redirect then I suggest you add ob_start(); at the beginning of PHP script.
refers this answer
Will all code after redirect header in PHP always get executed?
Using the header function in PHP only adds to the headers of the response returned by the server. It does not immediately send any data and does not immediately terminate the connection. Any code after the header
call will be executed.
In particular, it's a good idea to add a response body even after doing a 301 redirect so that clients that do not support the redirect also get some descriptive response. Infact according to the HTTP 1.1 specification Section 10.3.2 -
Unless the request method was HEAD, the entity of the response SHOULD
contain a short hypertext note with a hyperlink to the new URI(s). If
the 301 status code is received in response to a request other than
GET or HEAD, the user agent MUST NOT automatically redirect the
request unless it can be confirmed by the user, since this might
change the conditions under which the request was issued.
Related Topics
Change Woocommerce Default Password Security Level
PHP Difference Between Notice and Warning
How to Use Prepared Statements with Postgres
Create Superglobal Variables in PHP
Navigate Back with PHP Form Submission
How to Capture Search Term from Google Search
JSON: PHP to JavaScript Safe or Not
How to Decode Numeric HTML Entities in PHP
How to Convert a String to an Array in PHP
Flat PHP Array to Hierarchy Tree
Subtracting a Certain Number of Hours, Days, Months or Years from Date
Laravel Form HTML with Put Method for Put Routes
What Is the Safest Way of Passing Arguments from Server-Side PHP to Client-Side JavaScript
How to Clear PHP's Gettext Cache Without Restart Apache Nor Change Domain
What's the Best Way to Get the Fractional Part of a Float in PHP