Skip to content

Instantly share code, notes, and snippets.

@daveajones
Created July 21, 2016 14:56
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save daveajones/93c2401d263762b5327bb3be603db254 to your computer and use it in GitHub Desktop.
Save daveajones/93c2401d263762b5327bb3be603db254 to your computer and use it in GitHub Desktop.
A php function to follow all redirects and return the final destination url.
//Follow redirects to get to the final, good url
function get_final_url($url, $timeout = 5, $count = 0)
{
$count++;
$url = clean_url($url);
$ua = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0';
$cookie = tempnam("/tmp", "CURLCOOKIE");
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERAGENT, $ua);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
$content = curl_exec($curl);
$response = curl_getinfo($curl);
curl_close($curl);
unlink($cookie);
//loggit(3, "DEBUG: get_final_url($url) returned: [".$response['http_code']."]");
//Normal re-direct
if ($response['http_code'] == 301 || $response['http_code'] == 302 || $response['http_code'] == 303) {
//loggit(3, "DEBUG: ".print_r($response, TRUE));
ini_set("user_agent", $ua);
$headers = get_headers($response['url']);
$location = "";
foreach ($headers as $value) {
//loggit(3, "HEADER: [[".trim(substr($value, 9, strlen($value)))."]]");
if (substr(strtolower($value), 0, 9) == "location:") {
//loggit(3, "DEBUG: This was a normal http redirect.");
loggit(3, "HEADER: [[".trim(substr($value, 9, strlen($value)))."]]");
return get_final_url(trim(substr($value, 9, strlen($value))), 8, $count);
}
}
}
//Meta-refresh redirect
if (preg_match("/meta.*refresh.*URL=.*(http[^'\"]*)/i", $content, $value)) {
loggit(3, "DEBUG: This was a meta-refresh redirect.");
if (strpos($value[1], "http") !== FALSE) {
return get_final_url($value[1]);
}
}
//Javascript re-direct
if (preg_match("/window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/window\.location\=\"(.*)\"/i", $content, $value)) {
//loggit(3, "DEBUG: This was a javascript redirect.");
if (strpos($value[1], "http") !== FALSE) {
return get_final_url($value[1]);
} else {
return $response['url'];
}
} else {
//loggit(3, "DEBUG: No redirection.");
return $response['url'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment