I'm using the following functions to get the final URL from a series of redirects...
https://stackoverflow.com/a/4102293/1183476
It works great 99.8% of the time. I can't really pinpoint the exception, but I believe it has something to do with the server on the other end generating a new random URL for each visit. Thus, this script gets stuck in an infinite loop.
To replicate the issue replace the get_redirect_url
function with...
function get_redirect_url($url){
return $url.'x';
}
The Question
How can I set a time or iteration limit?
I feel like I've tried everything. I tried putting a time based condition in the while loop that looks for the next URL, but its not working and I don't know why. Like this...
function get_all_redirects($url){
$redirects = array();
$start = time();
while ($newurl = get_redirect_url($url) && time()-$start < 10 ){
if (in_array($newurl, $redirects)){
break;
}
$redirects[] = $newurl;
$url = $newurl;
}
return $redirects;
}
I also tried counting iterations like this...
function get_all_redirects($url){
$redirects = array();
$i = 0;
while ($newurl = get_redirect_url($url) && $i < 10 ){
if (in_array($newurl, $redirects)){
break;
}
$redirects[] = $newurl;
$url = $newurl;
$i++;
}
return $redirects;
}
The examples above are just 2 of many failed attempts. I'm ready for help. Thanks in advance.
';` to see why is it not finishing. – Ranty Mar 13 '13 at 08:53