PHP obtains the true URL address after 301 redirection page jump

Source: Internet
Author: User
Today, I saw a friend using phpsocket to obtain the address after the 301 jump. In fact, we still have a very simple method, that is, using the phpget_headers () function to obtain the number of http header information, let's take a look at our... today, I saw a friend using php socket to get the address after the 301 jump. In fact, we still have a very simple method, that is, using php get_headers () the function obtains the number of http headers. let's take a look at our implementation method.

The code is as follows:

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;  } }

Supplement: get_headers obtains all the headers sent by the server in response to an HTTP request. the code is as follows:

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

The code is as follows:

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 )


Article address:

Reprint at will ^ please include the address of this article!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.