How to call a redirect before the PHP script finishes?

I want to run a PHP script that will send a bunch of letters (newsletters), but since it may take some time, I want the user to be immediately redirected to a page with a message like "Your newsletters are queued for sending."
So far, redirection of headers works, but they do not cause redirection until all letters have been sent. Can I close the connection after sending the redirect so that the browser redirects immediately? I tried such things

ob_start(); ignore_user_abort(true); header( "refresh:1;url=waitforit.php?msg=Newsletters queued up, will send soon."); header("Connection: close"); header("Content-Length: " . mb_strlen($resp)); echo $resp; //@ob_end_clean(); ob_end_flush(); flush(); 

in various permutations and combinations, but to no avail. I suspect this cannot be done so simply, but before I start messing with cron jobs or user daemons, I thought I'd consider this approach. Thanks

like for example a blockhead but no luck

 ob_start(); ignore_user_abort(true); header( "refresh:1;url=mailing_done.php?msg=Newsletters queued for sending."); header("Connection: close"); header("Content-Length: 0" ); //echo $resp; ob_end_flush(); flush(); 
+8
redirect php header
source share
8 answers

If you want to run the script sequentially and still want to display some output, why don't you use the ajax request to the server, which can send mail to the queue and still allow the user to continue browsing this page.

If you do not want to use this; instead, you can use the script to run the wallpaper, even if the user is redirected to the 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 closed header("Connection: close"); //Ignore the user 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 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, //....... //....... ?> 
+12
source share

I would try this:

 <?php header("Location: waitforit.php?msg=Newsletters queued up, will send soon."); header("Content-Length: 0"); header("Connection: close"); flush(); //Do work here ?> 
+5
source share

Here is the right way to do this ... Sonia Desbiens, aka code: fataqui 01/07/2005

 <?php set_time_limit ( 0 ); /* start the forced redirect */ header ( 'Connection: close' ); ob_start (); /* close out the server process, release to the client */ header ( 'Content-Length: 0' ); header ( 'Location: /page_to_redirect_to.php' ); ob_end_flush (); flush (); /* end the forced redirect and continue with this script process */ ignore_user_abort ( true ); /* the rest of your script here */ /* * this example will redirect the user to '/page_to_redirect_to.php' * then this script will continue by sleeping for '10' seconds, then * this script will write a file, and then this script will exit... */ sleep ( 10 ); file_put_contents ( './out.txt', '' ); exit (); ?> 
+3
source share

Try setting Content-Length to 0

+1
source share

Why don't you use Location instead of refresh to redirect.

+1
source share

To make this work on IIS7, you need to use isapi with responseBufferLimit="0" in your web.config and output_buffering = Off in php.ini, then something like this should work ...

  session_write_close(); header( "Location: ".$sURL ) ; // try to allow continued processing if(ob_get_length()) ob_end_clean(); header("Connection: close"); ignore_user_abort(); // optional ob_start(); header("Content-Length: 0"); ob_end_flush(); // Strange behaviour, will not work flush(); // Unless both are called ! 
+1
source share

the accepted answer did not work for me, but it was done. Note that ignore_user_abort () must be included in php.ini

 ignore_user_abort(true); session_write_close(); // optional, this will close the session. header("Location: $url"); header("Content-Length: 0"); ob_end_flush(); flush(); //do_function_that_takes_five_mins(); 

http://waynepan.com/2007/10/11/how-to-use-ignore_user_abort-to-do-process-out-of-band/

+1
source share

Basically, you are looking for asynchronous job execution. I would ask the php script to create another thread (or process) that sends emails asynchronously. Thus, the thread that runs in the request is not tied to message processing. The thread that processes the request and returns the result can work completely separately from the thread that sends emails.

This thread seems to touch on this approach, it may be more useful in terms of implementation details: Asynchronously execute a PHP task

0
source share

All Articles