首页建站经验 php 获取301跳转后真实的url

php 获取301跳转后真实的url

现介绍利用get_headers() 函数获取http头php 自带的get_headers()取得服务器响应一个 HTTP 请求所发送的所有标头。 获取301状态肯定没问题。301定向的例子:google.com 会301…

现介绍利用get_headers() 函数获取http头

php 自带的get_headers()取得服务器响应一个 HTTP 请求所发送的所有标头。 获取301状态肯定没问题。

301定向的例子:

google.com 会301跳转至 www.google.com

再www.google.com 会302跳转至 www.google.com.hk

我写了个php函数 其php函数作用:

输入 google.com 得到 www.google.com.hk

输入 www.google.com 得到 www.google.com.hk

输入 www.google.com.hk 得到 www.google.com.hk

<?php/* @param str $url 查询 $return str 定向后的url的真实url */function getrealurl($url){ $header = get_headers($url,1); if (strpos($header[0],'301') || strpos($header[0],'302')) { if(is_array($header['Location'])) { return $header['Location'][count($header['Location'])-1]; }else{ return $header['Location']; } }else { return $url; }}$url = 'http://google.com';$url = getrealurl($url);echo '真实的url为:'.$url;//真实的url为:http://www.google.com.hk/?>

 

 

本文来自网络,不代表1号站长-站长学院|资讯交流平台立场。转载请注明出处: https://www.1cn.cc/jianzhan/jingyan/17431.html
上一篇php错误_date(): It is not safe to rely on the system's timezon
下一篇 jQuery获取页面元素绝对与相对位置的方法
admin

作者: admin

这里可以再内容模板定义一些文字和说明,也可以调用对应作者的简介!或者做一些网站的描述之类的文字或者HTML!

为您推荐

评论列表()

    联系我们

    联系我们

    0898-88888888

    在线咨询: QQ交谈

    邮箱: email@wangzhan.com

    工作时间:周一至周五,9:00-17:30,节假日休息

    关注微信
    微信扫一扫关注我们

    微信扫一扫关注我们

    关注微博
    返回顶部