How to get the redirected url? for example http://fromdomain.com/redirect.php?id=xxx redirected to http://todomain.com or http://todomain.com/page.htm where fromdomain.com is not my site, is it possible to get the redirected url using php?
function get_final_location($url) { $headers = @get_headers($url); foreach ((array)$headers AS $header) { if (preg_match('/Location\s*:\s*(https?:[^;\s\n\r]+)/i', $header, $redirect)) { return get_final_location($redirect[1]); } } return $url; } PHP: This gets the final URL, even if the page is redirected multiple times.
Does the following function works for you? I grabbed it instantly from php.net Should be abled to work on PHP4. <?php if(!function_exists('get_headers')) { function get_headers($url,$format=0) { $url=parse_url($url); $end = "\r\n\r\n"; $fp = fsockopen($url['host'], (empty($url['port'])?80:$url['port']), $errno, $errstr, 30); if ($fp) { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: ".$url['host']."\r\n"; $out .= "Connection: Close\r\n\r\n"; $var = ''; fwrite($fp, $out); while (!feof($fp)) { $var.=fgets($fp, 1280); if(strpos($var,$end)) break; } fclose($fp); $var=preg_replace("/\r\n\r\n.*\$/",'',$var); $var=explode("\r\n",$var); if($format) { foreach($var as $i) { if(preg_match('/^([a-zA-Z -]+): +(.*)$/',$i,$parts)) $v[$parts[1]]=$parts[2]; } return $v; } else return $var; } } } ?> PHP:
Another alternative is to use curl, it tells you stats like how long it took and where it ended up with curl_getinfo after your curl_exec call
Is this script to achieve http://mydomain.com/go.php?http://microsoft.com Basically I want all links that users click should go through my home url. Makes sense ?