follow a url?
Posted: Sun Jul 23, 2006 7:55 pm
Is it posible with cURL or something else that would allow you to type in a URL and see all of the pages that url redirects to?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Hmm.. the only problem is that potentially the redirect url could be different if you hit the url exactly.. such as sending a new request to the new location....Jcart wrote:hmm.. this is kind of a long shot (I know feyd has something better in mind) but try
1. turning off followurl to cancel any auto redirects
2. record the header location
3. send a new request to that location
4. record any furthur headers sent
5. repeat until no location headers present
This is assuming they are not using meta redirects.
Code: Select all
<?php
function list_redirects($url)
{
static $location;
$location = is_array($location) ? $location : array();
$url_parsed = parse_url($url);
extract($url_parsed);
if (!@$scheme)
{
$url = 'http://'.$url;
$url_parsed = parse_url($url);
}
$location[] = $url;
extract($url_parsed);
if(!@$port) $port = 80;
if(!@$path) $path = '/';
if(@$query) $path .= '?'.$query;
$out = "HEAD $path HTTP/1.0\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: Close\r\n\r\n";
if(!$fp = @fsockopen($host, $port, $es, $en, 5))
{
return false;
}
fwrite($fp, $out);
while (!feof($fp))
{
$s = fgets($fp, 128);
if(preg_match('/^Location:/', $s) !== 0)
{
fclose($fp);
return list_redirects(trim(preg_replace("/Location:/i", "", $s)));
}
if(preg_match('/^HTTP(.*?)200/i', $s))
{
fclose($fp);
return $location;
}
}
fclose($fp);
return false;
}
?>bokehman wrote:Sorry if this is a bit untidy but I only had 5 minutes to spare. Returns an array of URLs on success or false on failure.Code: Select all
<?php function list_redirects($url) { static $location; $location = is_array($location) ? $location : array(); $url_parsed = parse_url($url); extract($url_parsed); if (!@$scheme) { $url = 'http://'.$url; $url_parsed = parse_url($url); } $location[] = $url; extract($url_parsed); if(!@$port) $port = 80; if(!@$path) $path = '/'; if(@$query) $path .= '?'.$query; $out = "HEAD $path HTTP/1.0\r\n"; $out .= "Host: $host\r\n"; $out .= "Connection: Close\r\n\r\n"; if(!$fp = @fsockopen($host, $port, $es, $en, 5)) { return false; } fwrite($fp, $out); while (!feof($fp)) { $s = fgets($fp, 128); if(preg_match('/^Location:/', $s) !== 0) { fclose($fp); return list_redirects(trim(preg_replace("/Location:/i", "", $s))); } if(preg_match('/^HTTP(.*?)200/i', $s)) { fclose($fp); return $location; } } fclose($fp); return false; } ?>