Summary of several issues to pay attention to when using PHP Header for page jumps
Author:Eve Cole
Update Time:2009-06-05 16:25:10
When using header("location:test.php") to jump in PHP, you should pay attention to the following points, which will help solve some problems that novices often encounter.
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 the redirection 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.
like
<html><head></head><body>
<%response.redirect "../test.asp"%>
</body></html>
Check whether the following code in php will report an error:
<html><head></head><body>
<?
header("location:../test.php");
?>
</body></html>
This is the only way:
<?
header("location:../test.php");
?>
<html><head></head><body>...</body></html>
That is, the header function cannot send any data to the client before.
Example 2:
in asp
<html><head></head><body>
<%
response.redirect "../a.asp"
response.redirect "../b.asp"
%>
</body></html>
The result is a redirection of the a.asp file.
What about php?
<?
header("location:../a.php");
header("location:../b.php");
?>
<html><head></head><body></body></html>
We found that it redirects b.php.
It turns out that after executing redirect in asp, the subsequent code will not be executed.
After php executes the header, it continues to execute the following code.
In this regard, header redirection in php is not as good as 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 problems may occur if Unicode (UTF-8) is used to encode, 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>
var url = location.href;
if(url.indexOf('http://www.devdao.com/')!=-1)location.href='/index/index.htm';
if(url.indexOf('http://www.knowsky.com/')!=-1)location.href='/index1/index.htm';
if(url.indexOf('http://www.google.com/')!=-1)location.href='/cn/index.asp';
if(url.indexOf('http://www.baidu.com/')!=-1)location.href='/cn/index.asp';
</script>