Home > Article > Backend Development > What are the precautions for using PHP Header to jump to pages?
Notes on using PHP Header for page jumps: 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. After the header The PHP code will also be executed.
Notes on PHP Header used for page jumps:
The following is the redirection in asp## Comparison of #response.redirect:
Example 1:
response.redirect "../test.asp" header("location:../test.php");The difference between the two:asp’s
redirectThe function can work after sending the header file to the client.
<html><head></head><body> <%response.redirect "../test.asp"%> </body></html>If you check the following example code in php, an error will be reported:
<html><head></head><body> <? header("location:../test.php"); ?> </body></html>This can only be done:
<? header("location:../test.php"); ?> <html><head></head><body>...</body></html>That is, the header function cannot send any data to the customer before.
Example 2:<html><head></head><body>
<%
response.redirect "../a.asp"
response.redirect "../b.asp"
%>
</body></html>
The result in
a.aspWhere is the file.
<? header("location:../a.php"); header("location:../b.php"); ?> <html><head></head><body></body></html>We found that it redirected
b.php.
if(...) header("..."); else { ... }but we can simply use the following method:
if(...) { header("...");exit();}It should also be noted that if you are encoding with Unicode (UTF-8), problems will also occur, 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.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>
Related learning recommendations:php programming(video)
The above is the detailed content of What are the precautions for using PHP Header to jump to pages?. For more information, please follow other related articles on the PHP Chinese website!