Maison 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; 
 } 
}
Copier après la connexion

补充: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 
)
Copier après la connexion


文章地址:

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

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques mois By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Meilleurs paramètres graphiques
1 Il y a quelques mois By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Comment réparer l'audio si vous n'entendez personne
1 Il y a quelques mois By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Commandes de chat et comment les utiliser
1 Il y a quelques mois By 尊渡假赌尊渡假赌尊渡假赌

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)