首页 php教程 php手册 PHP获取301重定向页面跳转后真实URL地址

PHP获取301重定向页面跳转后真实URL地址

May 24, 2016 pm 01:25 PM

今天看到一个朋友利用php socket来获取的301跳转之后地地址了,其实我们还有一个非常简单办法了,就是使用php get_headers()函数获取数获取http头信息了,下面来看看我们的实现方法.

获取301状态肯定没问题,代码如下:

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; 
 } 
}
登录后复制

补充:get_headers,取得服务器响应一个 HTTP 请求所发送的所有标头,代码如下:

$url = 'http://phprm.com';print_r(get_headers($url));

结果,代码如下:

Array 
( 
    [0] => HTTP/1.1 301 Moved Permanently 
    [1] => Date: Tue, 01 Jul 2014 07:49:26 GMT 
    [2] => Server: Apache/2.2.22 (Win32) PHP/5.2.17 
    [3] => Location: http://www.phprm.com/ 
    [4] => Content-Length: 314 
    [5] => Content-Type: text/html; charset=iso-8859-1 
    [6] => X-Via: 1.1 jszjsx60:8080 (Cdn Cache Server V2.0), 1.1 zb51:6 (Cdn Cache Server V2.0) 
    [7] => Connection: close 
    [8] => HTTP/1.1 200 OK 
    [9] => Date: Tue, 01 Jul 2014 07:49:27 GMT 
    [10] => Server: Apache/2.2.22 (Win32) PHP/5.2.17 
    [11] => Last-Modified: Tue, 01 Jul 2014 07:41:43 GMT 
    [12] => ETag: "7a0000002fe1a1-68a9-4fd1ce83bc0f7" 
    [13] => Accept-Ranges: bytes 
    [14] => Content-Length: 26793 
    [15] => Content-Type: text/html 
    [16] => X-Via: 1.1 jszjsx60:8080 (Cdn Cache Server V2.0), 1.1 zb62:5 (Cdn Cache Server V2.0) 
    [17] => Connection: close 
)
登录后复制


文章地址:

转载随意^^请带上本文地址!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)