The code is as follows:
/**
* Get_redirect_url ()
* Gets The address of the provided URL redirects to,
* or FALSE if there ' s no redirect.
*
* @param string $url
* @return String
*/
function Get_redirect_url ($url) {
$redirect _url = null;
$url _parts = @parse_url ($url);
if (! $url _parts) return false;
if (!isset ($url _parts[' host ')) return false; Can ' t process relative URLs
if (!isset ($url _parts[' path ')) $url _parts[' path ' = '/';
$sock = Fsockopen ($url _parts[' host '), (Isset ($url _parts[' Port ')? (int) $url _parts[' Port ']: $errno, $ERRSTR, 30);
if (! $sock) return false;
$request = "Head". $url _parts[' path ']. (Isset ($url _parts[' query ')? '?'. $url _parts[' query ']: ". "Http/1.1rn";
$request. = ' Host: '. $url _parts[' host ']. "RN";
$request. = "Connection:closernrn";
fwrite ($sock, $request);
$response = ';
while (!feof ($sock)) $response. = Fread ($sock, 8192);
fclose ($sock);
if (Preg_match ('/^location: (. +?) $/m ', $response, $matches)) {
if (substr ($matches [1], 0, 1) = = "/")
return $url _parts[' scheme '. "://" . $url _parts[' host ']. Trim ($matches [1]);
Else
return Trim ($matches [1]);
} else {
return false;
}
}
/**
* get_all_redirects ()
* Follows and collects all redirects, in order, for the given URL.
*
* @param string $url
* @return Array
*/
function Get_all_redirects ($url) {
$redirects = Array ();
while ($newurl = Get_redirect_url ($url)) {
if (In_array ($newurl, $redirects)) {
break;
}
$redirects [] = $newurl;
$url = $newurl;
}
return $redirects;
}