繁体   English   中英

PHP get_headers递归

[英]PHP get_headers recursively

我正在尝试实现标头响应以递归地跟随标头重定向。 我实现了以下内容,该内容对于第一个请求正确运行,但是如果在标头中找到位置重定向,则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;
}

任何想法? 我有一个在线实施...如果需要查看有效的代码。 谢谢

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

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上检查工作脚本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM