lost baby
lost baby

Reputation: 3268

How to cause a redirect to occur before php script finishes?

I want to run a php script that will send out a bunch of emails (newsletters) but since it can take a while for that to run I want the user be immediately redirected to a page with a message like "Your newsletters are queued to be sent out."
So far the header redirects work but they do not cause the redirection until after all the emails get sent. Can I close the connection after sending the redirect so the browser acts on the redirect immediately? I tried stuff like this


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 it cannot be done so simply but before I start messing with cron jobs or custom daemons I thought I'd look into this approach. Thanks

eg like blockhead suggests below, 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(); 

Upvotes: 10

Views: 5819

Answers (7)

Bhavesh G
Bhavesh G

Reputation: 3028

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,
    //.......
    //.......
?>

Upvotes: 15

Doug Cassidy
Doug Cassidy

Reputation: 1907

the accepted answer didnt work for me, but this did. Please note that the ignore_user_abort() must be turned on 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/

Upvotes: 1

John
John

Reputation: 31

Here is the proper way to do it... code by Sonia Desbiens, aka: fataqui 07/01/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 ();

?>

Upvotes: 3

To get this to work on IIS7 you need to be using 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 !

Upvotes: 1

Localghost
Localghost

Reputation: 712

Basically, you are looking to asynchronously perform the job. I would have the php script create another thread (or process) that sends the emails asynchronously. That way, the thread that the request is running in isn't tied to the processing of the emails. The thread that is processing the request and returning the result can operating completely separately from the thread that is sending the emails.

This thread seems to be touching upon this approach, perhaps it will be more helpful regarding implementation details: Run PHP Task Asynchronously

Upvotes: 0

Phil W
Phil W

Reputation: 549

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
?>

Upvotes: 7

blockhead
blockhead

Reputation: 9705

Try setting Content-Length to 0

Upvotes: 1

Related Questions