2015-10-07 67 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

檢查工作腳本