Tips for Using headers in PHP. We cannot have spaces between 1, location, and:. Otherwise, an error will occur. 2. there cannot be any output before using the header. 3. the PHP code after using the header in PHP will also be executed. Below is1. there must be no space between location and:. Otherwise, an error will occur.
2. there cannot be any output before using the header.
3. the PHP code after using the header in PHP will also be executed.
The following is a comparison with response. redirect in asp:
Example 1:
Response. redirect ".../test. asp"
Header ("location: ../test. php ");
Differences:
The redirect function of asp can work after the header file is sent to the customer.
For example
- <html><head> head><body>
- <%response.redirect "../test.asp"%>
- body> html>
The following code in the header in PHP reports 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, no data can be sent to the customer before the header function is used in PHP.
Example 2:
Asp
- <html><head> head><body>
- <%
- response.redirect "../a.asp"
- response.redirect "../b.asp"
- %>
- body> html>
The result is to redirect the. 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 redirect is executed in asp, subsequent code will not be executed.
Php continues to execute the following code after executing the header.
In this regard, the header redirection in php is not as good as the redirection in asp. sometimes, after we want to redirect, we cannot execute the following code:
Generally, we use
- if(...)
- header("...");
- else
- {
- ...
- }
However, we can simply use the following method:
- if(...)
- { header("...");exit();}
In PHP, the header should also be noted that if Unicode (UTF-8) encoding will also occur, 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>
When 1, there cannot be spaces between location and:; otherwise, an error will occur. 2. there cannot be any output before using the header. 3. the PHP code after using the header in PHP will also be executed. Below is...