按照URL状态获取302、301重定向后的URL网址

许多的网站都利用了URL重定向,这就导致了收罗到URL时HTML状态位为302、301的错误。为了制止这个错误,需要判定html头(header) 的信息,当html状态是301或302时,url就不是本来的谁人。利用以下的函数可以按照你查询的UR获得真实的URL。

再从真实的URL举办收罗。

<?php function realurl($hosturl) { $header = get_headers($hosturl, 1); echo 'html状态:' ; $righturl=''; if( $header[0]=='HTTP/1.0 302 Found'|| $header[0]=='HTTP/1.0 302 Moved Temporarily'|| $header[0]=='HTTP/1.0 301 Moved Permanently'|| $header[0]=='HTTP/1.0 302 Object moved'|| $header[0]=='HTTP/1.1 302 Found'|| $header[0]=='HTTP/1.1 302 Moved Temporarily'|| $header[0]=='HTTP/1.1 301 Moved Permanently'|| $header[0]=='HTTP/1.1 302 Object moved') { $righturl = (strpos($header['Location'],'/')==0) ? $hosturl.$header['Location']: $header['Location']; echo '重定向<br />真实的URL为:'.$righturl; }elseif($header[0]=='HTTP/1.1 200 OK'||$header[0]=='HTTP/1.0 200 OK') { echo '<font color="#666633">200 OK</font>'.'<br />'; $righturl= $hosturl; }else { echo '<font color="#ff0033">'.$header[0].'</font>'; exit('<br />url无法理会'); } if(is_array($righturl)) { $righturl = $righturl[0]; echo 'OK 就是你了'.$righturl; return $righturl; }else { return false; } } //以下为测试 $hosturl = 'http://www.enenba.com/'; realurl($hosturl); /* output html状态:重定向 真实的URL为: */ ?>

想利用的话就去掉输出部门就行了,函数返回正确的URL。我

临时还没发明什么漏掉,

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/7919.html