PHP Header for page jump several issues to note summary _php skills

Source: Internet
Author: User

1.header () function

The header () function is a very simple way to make a page jump in PHP. The main function of the header () function is to output HTTP protocol headers (headers) to the browser.

The header () function is defined as follows:

void header (String string [, bool replace [, int http_response_code]])
Optional parameter replace indicates whether to replace the previous similar header or to add a phase (www.jb51.net) of the same type of header, default to replace.

The Second optional parameter Http_response_code forces the HTTP corresponding code to the specified value. The header of the location type in the header function is a special header invocation that is commonly used to implement page jumps. Attention:

1.location and ":" No space between, otherwise will not jump.
2. You cannot have any output before using the header.
The PHP code after 3.header will also be executed. For example, redirect the browser to Jb51.net

<?php 
 //Redirect Browser 
header ("location:http://www.jb51.net"); 
 Ensure that after redirection, subsequent code will not be executed 
exit;
? >

1, PHP Jump code A sentence-style:

<?php 
$url = $_get[' url '];
Header ("Location: $url");
? >

2, PHP jump code if the judge-type:

Copy Code code as follows:

if ($_cookie["U_type"]) {header (' location:register.php ');} else{Setcookie (' U_type ', ' 1 ', ' 86400*360 '); Set the cookie long valid header (' location:zc.html ');

Note: Save as zc.php, when the user accesses the zc.php, judge whether a cookie exists, if it is saved (www.jb51.net) jumps to register.php, if it does not exist, create a cookie and then jump to the Zc.htmlfrom:http ://www.jb51.net/phper/php-cy/62883.htm

URL redirection function

URL redirection
function redirect ($url, $time =0, $msg = ") {
 //multiline URL address support
$url = str_replace (Array (" n "," R "),", $url );
 if (empty ($msg))
 $msg = "The system will automatically jump to {$time} seconds after {$url}! ";
if (!headers_sent ()) {
 //redirect
 if (0 = = $time) {
 header (' Location: '. $url);
 } else {
 header (" refresh:{$time};url={$url} ");
Echo ($msg);
 Exit ();
 } else {
 $str = ' <meta http-equiv= ' Refresh ' content= ' {$time}; url={$url} ' > ';
if ($time!= 0)
 $str. = $msg;
 Exit ($STR);
 }
 }

The above can not return 404 status, if it is a page jump after returning 404 status Code We can do the following

function Getref ()
 {
 $url = @$_server[' Http_referer '];
 if (!empty ($url))
 {
 if (!strstr ($url, ' jb51.net ') &&!strstr ($url, ' jb51.net '))
 {
 @header (" http/1.1 404 Not Found ");
 @header ("status:404 not Found");
 Include ("404.html");//Jump to a page, recommend using this method
 exit ();
 }
 else
 {
 @header ("http/1.1 404 Not Found");
 @header ("status:404 not Found");
 Include ("404.html");//Jump to a page, recommend using this method
 exit ();
 }
 

If you want to do 301, it's pretty much

<?php 
 $the _host = $_server[' http_host '];
 $request _uri = isset ($_server[' Request_uri '))? $_server[' Request_uri ']: ';
 if ($the _host!== ' www.jb51.net ') {//echo $_server[' http_host '].$_server[' php_self '
  ];
  Header (' http/1.1 moved Permanently ');
  Header (' location:http://www.jb51.net '. $_server[' php_self ']. $request _uri);
 >

The following is a comparison with the redirect Response.Redirect in asp:
Example 1:


Response.Redirect ". /test.asp "


Header ("Location:.") /test.php ");


The difference between the two:


The redirect function of an ASP can work after sending a header file to a customer.


Such as


&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;


&lt;%response.redirect ". /test.asp "%&gt;


&lt;/body&gt;&lt;/html&gt;


Check that the next code in PHP will report an error:


&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;


?


Header ("Location:.") /test.php ");


?&gt;


&lt;/body&gt;&lt;/html&gt;


You can only do this:


?


Header ("Location:.") /test.php ");


?&gt;


&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;...&lt;/body&gt;&lt;/html&gt;


That is, the header function cannot send any data to the customer.


Example 2:


in ASP


&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;


&lt;%


Response.Redirect ". /a.asp "


Response.Redirect ". /b.asp "


%&gt;


&lt;/body&gt;&lt;/html&gt;


The result is a redirected a.asp file.


What about PHP?


?


Header ("Location:.") /a.php ");


Header ("Location:.") /b.php ");


?&gt;


&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;


We found it redirected to b.php.


The following code is not executed after the redirect is executed in ASP.


and PHP continues to execute the following code after executing the header.


In this respect the header redirection in PHP is not as redirected as in ASP. Sometimes after we redirect, we can't execute the following code:


In general, we use


if (...)


Header ("...");


Else


{


...


}


But we can simply use the following methods:


if (...)


{Header ("..."); exit ();}


Also note that if you are encoding with Unicode (UTF-8), you also have problems, and you need to adjust the cache settings.


&lt;[email=%@]% @LANGUAGE = "Vbscript[/email]" codepage= "936"%&gt;


&lt;%if request.servervariables ("server_name") = "S.jb51.net" Then


Response.Redirect "News/index.htm"


Else%&gt;


&lt;%end if%&gt;


&lt;script&gt;


var url = location.href;


if (Url.indexof (' http://www.jb51.net/')!=-1) location.href= '/index/index.htm ';


if (Url.indexof (' http://www.kanshule.com/')!=-1) location.href= '/index1/index.htm ';


if (Url.indexof (' http://www.shouji17.com/')!=-1) location.href= '/cn/index.asp ';


if (Url.indexof (' http://www.baidu.com/')!=-1) location.href= '/cn/index.asp ';


&lt;/script&gt;

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.