What are the precautions for using PHP Header to jump to pages?

coldplay.xixi
Release: 2023-03-04 08:34:01
Original
1938 people have browsed it

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.

What are the precautions for using PHP Header to jump to pages?

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");
Copy after login

The difference between the two:

asp’s

redirectThe function can work after sending the header file to the client.

For example,

<html><head></head><body>
<%response.redirect "../test.asp"%>
</body></html>
Copy after login

If you check the following example code in php, an error will be reported:

<html><head></head><body>
<?
header("location:../test.php");
?>
</body></html>
Copy after login

This can only be done:

<?
header("location:../test.php");
?>
<html><head></head><body>...</body></html>
Copy after login

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>
Copy after login
The result in

asp is a redirect

a.aspWhere is the file.

php?

<?
header("location:../a.php");
header("location:../b.php");
?>
<html><head></head><body></body></html>
Copy after login

We found that it redirected

b.php.

originally in After executing redirect in asp, the following code will not be executed.

And php will continue to execute the following code after executing the header.

In this regard, header redirection in php is not as good as asp Redirection in. Sometimes we cannot execute the subsequent code after redirection:

Generally we use

if(...)
header("...");
else
{
...
}
Copy after login

but we can simply use the following method:

if(...)
{ header("...");exit();}
Copy after login

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(&#39;http://www.devdao.com/&#39;)!=-1)location.href=&#39;/index/index.htm&#39;;
if(url.indexOf(&#39;http://www.knowsky.com/&#39;)!=-1)location.href=&#39;/index1/index.htm&#39;;
if(url.indexOf(&#39;http://www.google.com/&#39;)!=-1)location.href=&#39;/cn/index.asp&#39;;
if(url.indexOf(&#39;http://www.baidu.com/&#39;)!=-1)location.href=&#39;/cn/index.asp&#39;;
</script>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template