2015-10-07 66 views
0

我试图实现一个头响应跟随recursevely头重定向。我已经实现了对第一个请求正确工作的以下内容,但是如果在头中找到位置重定向,则get_headers不会为重定向的位置返回任何结果。我想显示每个头申请的头。PHP get_headers递归

这就是我所做的。

function redirectURL($domain) { 

$newLocation = ''; 
$domain = str_replace("\r", "", $domain); 

$headers=get_headers($domain); 

echo "<ul class='list-group' >"; 

print "<li class='list-group-item'>".$domain. "</li>"; 
      foreach($headers as $k=>$v){ 
       print "<li class='list-group-item'>".$k . ": " . $v . "</li>"; 
       if(strstr($v, 'Location')){ 
        $location = explode(":",$v); 
        $newLocation = $location[1].":".$location[2]; 
       } 
      } 
echo "</ul>"; 

if($newLocation != $domainName && $newLocation != ''){ 
    redirectURL($newLocation); 
} 

unset($headers); 

return true; 
} 

任何想法?我有一个在线实现...如果需要看到一个工作代码。 谢谢

回答

0

好吧,这只是不好的编码。我已经做到了。 这是一个工作代码

function redirectURL($domainName) { 

$i=0; 
$newLocation = ''; 
$isNew = false; 
$headers = array(); 
$domainName = str_replace("\r", "", $domainName); 

$headers=get_headers($domainName,1); 



echo "<ul class='list-group' >"; 

print "<li class='list-group-item'><strong>".$domainName. "</strong></li>"; 
      foreach($headers as $k => $v){ 
       print "<li class='list-group-item'>".$k . ": " . $v . "</li>"; 

        if($k == 'Location'){ 

         $newLocation = $v; 
         $isNew = true; 
         print "<li class='list-group-item'><strong>".$k . ": " . $v . "</strong></li>"; 
        } 

      } 
echo "</ul>"; 

unset($headers); 
//limit recurse to $i < 4 to avoid overload 
if($isNew){ 
    $i++; 
    if($i<4) {redirectURL($newLocation);} 

} 

return true; 
} 

您可以在https://www.neting.it/risorse-internet/controlla-redirect-server.html

检查工作脚本