strange problem, i’m opening a connection with fsockopen() to a page,
that page has a header(location:xx) to the same page (i’m just refreshing the script), but it’seems that the redirect isn’t working…
obviously everything is working if i’m replicating it with a browser…
some code:
CONNECTION PAGE:
$socketcon = fsockopen($_SERVER['HTTP_HOST'],80,$errorno,$errorstr,10);
if($socketcon) {
$socketdata = "GET http://www.example.com/test2.php HTTP/1.0\r\nHost: ".$_SERVER['HTTP_HOST']."\r\nConnection: Close\r\n\r\n";
fwrite($socketcon,$socketdata);
fclose($socketcon);
}
CONNECTED PAGE (test2.php):
<?
//other code (working fine)
if($_GET["AAA"]){
//REDIRECT WORKED !
} else {
header("location:test2.php?AAA=1"); //same page but with a get param
}
?>
the REDIRECT WORKED part never get executed…
any suggestions ?
EDIT: the connection MUST be asynchronous
Consider Is making asynchronous HTTP requests possible with PHP? which is a similar question.
Using
exec()-family functions might be a good bet if you don’t need return values; you can just call curl appended by&and it’ll run in the background.If you really, truly want to do this in PHP, you can either: spawn a new process (using
pcntl_fork()) or handle redirects yourself.To handle redirects you’ll need to:
/\s*location:\s*(.+)$/m, I believe.parse_url()may come in handy, but does not check for potential security issues.You can see this is very complex. This will also be recursive. What you’re really talking about doing is writing a rudimentary HTTP parser in PHP. This is not a good idea – use cURL and find some other way to make it asynchronous.