Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Redirect User and Continue Process Script [closed]

Tags:

php

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);

?>
like image 582
Pracha Saensuk Avatar asked Nov 25 '22 19:11

Pracha Saensuk


1 Answers

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.

like image 82
DGM Avatar answered Dec 20 '22 15:12

DGM