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
<html><head></head><body>
<%response.redirect ". /test.asp "%>
</body></html>
Check that the next code in PHP will report an error:
<html><head></head><body>
?
Header ("Location:.") /test.php ");
?>
</body></html>
You can only do this:
?
Header ("Location:.") /test.php ");
?>
<html><head></head><body>...</body></html>
That is, the header function cannot send any data to the customer.
Example 2:
in ASP
<html><head></head><body>
<%
Response.Redirect ". /a.asp "
Response.Redirect ". /b.asp "
%>
</body></html>
The result is a redirected a.asp file.
What about PHP?
?
Header ("Location:.") /a.php ");
Header ("Location:.") /b.php ");
?>
<html><head></head><body></body></html>
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.
<[email=%@]% @LANGUAGE = "Vbscript[/email]" codepage= "936"%>
<%if request.servervariables ("server_name") = "S.jb51.net" Then
Response.Redirect "News/index.htm"
Else%>
<%end if%>
<script>
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 ';
</script>