Php URL redirection header () function

Source: Internet
Author: User

// URL redirection

Function redirect ($ url, $ time = 0, $ msg = "){

// Multi-line URL support

$ Url = str_replace (array ("\ n", "\ r"), ", $ url );

If (empty ($ msg ))

$ Msg = "The system will automatically jump to {$ url}! after {$ time} seconds }!";

If (! Headers_sent ()){

// Redirect

If (0 ===$ time ){

Header ('Location: '. $ url );

} Else {

Header ("refresh: {$ time}; url = {$ url }");

Echo ($ msg );

}

Exit ();

} Else {

$ Str = "";

If ($ time! = 0)

$ Str. = $ msg;

Exit ($ str );

}

}

// Url redirection 2

Function redirect ($ url ){

Echo"

";

Exit ();

}

HTTP header information

That is, use the php header function. The HEADER function in PHP is used to send control commands that should have been specified by the HTTP protocol to the browser through the WEB server, such as declaring the type of returned information ("Context-type: xxx/xxx "), page attributes (" No cache "," Expire "), and so on.

Use the HTTP header to redirect PHP to another page as follows:

<? Php

$ Url = "www.111cn.net ";

If (! Empty ($ url ))

{

Header ("HTTP/1.1 303 See Other"); // This statement can be left empty

Header ("Location: $ url ");

}

?>

Note that "Localtion:" is followed by a space, and a comprehensive function is organized below.

/**

* Get_redirect_url ()

* Gets the address that the provided URL redirects,

* Or FALSE if there's no redirect.

*

* @ Param string $ url

* @ Return string

*/

Function get_redirect_url ($ url ){

$ Redirect_url = null;

$ Url_parts = @ parse_url ($ url );

If (! $ Url_parts) return false;

If (! Isset ($ url_parts ['host']) return false; // can't process relative URLs

If (! Isset ($ url_parts ['path']) $ url_parts ['path'] = '/'

$ Sock = fsockopen ($ url_parts ['host'], (isset ($ url_parts ['port'])? (Int) $ url_parts ['port']: 80), $ errno, $ errstr, 30 );

If (! $ Sock) return false;

$ Request = "HEAD". $ url_parts ['path']. (isset ($ url_parts ['query'])? '? '. $ Url_parts ['query']: '')." HTTP/1.1rn ";

$ Request. = 'host: '. $ url_parts ['host']. "rn ";

$ Request. = "Connection: Closernrn ";

Fwrite ($ sock, $ request );

$ Response =''

While (! Feof ($ sock) $ response. = fread ($ sock, 8192 );

Fclose ($ sock );

If (preg_match ('/^ Location: (. + ?) $/M', $ response, $ matches )){

Return trim ($ matches [1]);

} Else {

Return false;

}

}

/**

* Get_all_redirects ()

* Follows and collects all redirects, in order, for the given URL.

*

* @ Param string $ url

* @ Return array

*/

Function get_all_redirects ($ url ){

$ Redirects = array ();

While ($ newurl = get_redirect_url ($ url )){

If (in_array ($ newurl, $ redirects )){

Break;

}

$ Redirects [] = $ newurl;

$ Url = $ newurl;

}

Return $ redirects;

}

/**

* Get_final_url ()

* Gets the address that the URL ultimately leads.

* Returns $ url itself if it isn't a redirect.

*

* @ Param string $ url

* @ Return string

*/

Function get_final_url ($ url ){

$ Redirects = get_all_redirects ($ url );

If (count ($ redirects)> 0 ){

Return array_pop ($ redirects );

} Else {

Return $ url;

}

}

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.