5

I want script to redirect user as fast as it can and continue process data after user was redirected. I found this on google but It's doesn't work. It wait for 10 second before redirect.It's seem that user need to wait till script is finish before browser can be direct.

<?PHP
 store data
 $userAgent = $_SERVER['HTTP_USER_AGENT'];
 $user_ip=$_SERVER['REMOTE_ADDR'];

 //Redirect to google
 header("Location: http://www.google.com");
 //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();
//Yes... flush again.
flush();

//Close the session.
session_write_close();

//After redirection no need to send any data to user anymore.
//User would It's seem that user need to wait till script is finish before browser can be direct.
//Do some work 
//sleep(10);
$matched = $wurflObj->getDeviceCapabilitiesFromAgent($userAgent);
$org = geoip_org_by_name($user_ip);

?>
  • Remove sleep(10)...what its been doing there!!!!! – perilbrain Aug 05 '12 at 17:57
  • 1
    Folks I think the point he trying to make is that he wants the browser to move on while the heavy work being done is simulated by the `sleep 10`. Of course there's a relationship between the question and the sleep, but the question is why doesn't the browser mover on. – DGM Aug 05 '12 at 18:03
  • @DGM That correct! Do you know why user isn't redirect before sleep(10). – Pracha Saensuk Aug 05 '12 at 18:06
  • possible duplicate of [Can I cause a redirect to occur before my php script finishes?](http://stackoverflow.com/questions/10504753/can-i-cause-a-redirect-to-occur-before-my-php-script-finishes) – DGM Aug 05 '12 at 18:11
  • @DMG yes it is. but I still have the problem. I try their solution but didn't work – Pracha Saensuk Aug 05 '12 at 18:31

4 Answers4

1

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.

DGM
  • 26,629
  • 7
  • 58
  • 79
0

The reason that it waits is because of the line sleep(10). Remove that, and it should go at the speed you'd expect.

SomeKittens
  • 38,868
  • 19
  • 114
  • 143
  • 1
    Hi, I use sleep(10) to simulate script execution time. I want to make sure that user must be redirect before script is execute and finish. Do I need to use another function instead of sleep(10)? – Pracha Saensuk Aug 05 '12 at 18:02
0

The sleep(10) near the end of your code might just possibly be the cause.

If you want the work to be done in the background, you have several options.

Probably the best one would be to use AJAX from the client site to execute the heavy PHP script, and return the results when it's finished.

PHP alone cannot do asynchronous work like you want.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • 1
    Hi, I use sleep(10) to simulate script execution time. I want to make sure that user must be redirect before script is execute and finish. Do I need to use another function instead of sleep(10)? – Pracha Saensuk Aug 05 '12 at 18:08
  • Just do your work and redirect immediately after you finish. Don't blindly wait for a set amount of time and hope the script finished by then. – Madara's Ghost Aug 05 '12 at 18:09
  • I cant use ajax. Most of my user use mobile that not support ajax.The real purpose of script is to redirect user to the website and dont have to wait for script process. After script end, user will be at other website. – Pracha Saensuk Aug 05 '12 at 18:26
  • @PrachaSaensuk: PHP is not a suitable language to do it than. Look at asynchronous languages such as `Node.js`. – Madara's Ghost Aug 05 '12 at 18:37
  • After redirection no need to send any data to user anymore. I know that php can run in background even if browser has disconnect but I dont know how to disconnect browser from the server before script end. – Pracha Saensuk Aug 05 '12 at 18:44
  • @PrachaSaensuk: `die();` – Madara's Ghost Aug 05 '12 at 18:48
  • @Truth If use die(); "//before do some work" it wont do anything after die(); – Pracha Saensuk Aug 05 '12 at 18:50
-1
 Sleep(10)

is causing the 10 second wait. I'm not sure what it's even doing there. But remove that and you'll be fine!

I presume the 10 second wait was there as an example of "doing some work".

Jordan Richards
  • 532
  • 3
  • 18