PHP source code

Source: Internet
Author: User
In php, URL redirection is inseparable from header functions no matter which method. Below I will organize some common URL jump implementation programs and methods for you. If you need them, please refer to them. In php, URL redirection is inseparable from header functions no matter which method. Below I will organize some common URL jump implementation programs and methods for you. If you need them, please refer to them.

Script ec (2); script

1. header () function

The header () function is a very simple method for page Jump in PHP. The main function of the header () function is to output the HTTP header to the browser.

The header () function is defined as follows:

Void header (string [, bool replace [, int http_response_code])
The optional parameter replace indicates whether to replace the previous header or add a header of the same type. The default parameter is replace.

The second optional parameter http_response_code sets the HTTP code as the specified value. The Location header in the header function is a special header call, which is often used to redirect pages. Note:

1. No space is allowed between location and:; otherwise, no jump is made.

2. There cannot be any output before using the header.

3. the PHP code after the header will also be executed. For example, redirect the browser to linzl.com

The Code is as follows:

<? Php
// Redirect the browser
Header ("Location: http://www.111cn.net ");
// Ensure that subsequent code will not be executed after redirection
Exit;
?>


1. php jump code in one sentence:

The Code is as follows:

Note: If you save it as ad. php, You can implement ad. php? Effect of redirecting from url = www.111cn.net to idea network

2. php jump code if applicable:

The Code is as follows:

If ($ _ COOKIE ["u_type"]) {header ('location: register. php ');} else {setcookie ('U _ type', '1', '2014*86400'); // sets the long-term valid header ('location: zc.html ');}

Note: accept


URL redirection Function

The Code is as follows:

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

The above Code cannot return the 404 status. If the code returns the 404 status after the page Jump, perform the following operations:

The Code is as follows:

Function getref ()
{
$ Url = @ $ _ SERVER ['HTTP _ referer'];
If (! Empty ($ url ))
{
If (! Strstr ($ url, '111cn. net ')&&! Strstr ($ url, '111cn. net '))
{
@ Header ("http/1.1 404 not found ");
@ Header ("status: 404 not found ");
Include ("404.html"); // jump to a page. This method is recommended.
Exit ();
}
}
Else
{
@ Header ("http/1.1 404 not found ");
@ Header ("status: 404 not found ");
Include ("404.html"); // jump to a page. This method is recommended.
Exit ();
}
}

If you want to do 301, it's almost the same

The Code is as follows:

$ The_host = $ _ SERVER ['HTTP _ host'];
$ Request_uri = isset ($ _ SERVER ['request _ URI '])? $ _ SERVER ['request _ URI ']: '';
If ($ the_host! = 'Www .111cn.net ')
{
// Echo $ _ SERVER ['HTTP _ host']. $ _ SERVER ['php _ SELF '];
Header ('HTTP/1.1 301 Moved Permanently ');
Header ('location: http://www.111cn.net '. $ _ SERVER ['php _ SELF']. $ request_uri );
}
?>

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.