【问题标题】:PHP get_headers recursivelyPHP get_headers 递归
【发布时间】:2015-10-07 11:26:02
【问题描述】:

我正在尝试实现标头响应以跟踪递归标头重定向。我已经实现了以下对于第一个请求可以正常工作,但是如果在标头中找到位置重定向,则 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;
}

有什么想法吗?我有一个在线实现......如果需要查看工作代码。 谢谢

【问题讨论】:

    标签: php url-redirection get-headers


    【解决方案1】:

    好吧,这只是糟糕的编码。我已经让它工作了。 这是一个工作代码

    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查看工作脚本

    【讨论】:

      猜你喜欢
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 2019-02-24
      • 2016-03-15
      • 2012-08-10
      相关资源
      最近更新 更多