Home  >  Article  >  Backend Development  >  Summary of several issues to pay attention to when using PHP Header for page jump_PHP Tutorial

Summary of several issues to pay attention to when using PHP Header for page jump_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:49:51887browse

1. There cannot be a space between location and ":", otherwise an error will occur.
2. There cannot be any output before using the header.
3. The PHP code after the header will also be executed.
The following is a comparison with response.redirect in asp:
Example 1:
response.redirect "../test.asp"
header("location:../test.php ");
The difference between the two:
asp's redirect function can work after sending the header file to the client.
For example,
< body>
<%response.redirect "../test.asp"%>

Check if the following code in PHP will report an error:

header("location:../test.php");
?>

You can only do this:
header("location:../test.php");
?>
< ;html>...
That is, the header function cannot send any data to the client before.
Example 2:
asp in

<%
response.redirect "../a.asp"
response.redirect "../b.asp"
%>

The result is to redirect the a.asp file.
What about php?
< ?
header("location:../a.php");
header("location:../b.php");
?>
< head>
We found that it redirects b.php.
It turns out that after executing redirect in asp, the following code will not be executed. .
After executing the header, PHP continues to execute the following code.
In this regard, the header redirection in PHP is not as good as the redirection in ASP. Sometimes we cannot execute the following code after redirecting:
Generally we use
if(...)
header("...");
else
{
...
}
But We can simply use the following method:
if(...)
{ header("...");exit();}
Also note that if you use Unicode( UTF-8) encoding will also cause problems, and cache settings need to be adjusted.
<[email=%@]%@LANGUAGE="VBSCRIPT[/email]" CODEPAGE="936"%>
< ;%if Request.ServerVariables("SERVER_NAME")="s.jb51.net" then
response.redirect "news/index.htm"
else%>
<%end if%> ;
<script> <br>var url = location.href; <br>if(url.indexOf('http://www.jb51.net/')!=-1)location.href=' /index/index.htm'; <br>if(url.indexOf('http://www.kanshule.com/')!=-1)location.href='/index1/index.htm'; <br>if(url.indexOf('http://www.shouji17.com/')!=-1)location.href='/cn/index.asp'; <br>if(url.indexOf('http: //www.baidu.com/')!=-1)location.href='/cn/index.asp'; <br></script>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319488.htmlTechArticle1. There cannot be a space between location and ":", otherwise an error will occur. 2. There cannot be any output before using the header. 3. The PHP code after the header will also be executed. The following is the redirection in asp...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn