Summary of php header('location: $url') page jump precautions

伊谢尔伦
Release: 2023-03-11 08:46:02
Original
7435 people have browsed it

When using header("location:test.php") to jump in PHP, please 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 redirect response.redirect in asp:

Example 1:

response.redirect"../test.asp"
header("location:../test.php");
Copy after login

The difference between the two:

asp's redirect function can take effect after sending the header file to the customer.

For example,

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

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

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

This is the only way:

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

That is, no data can be sent to the client before the header function.

Example 2:

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

Result in

asp It redirects the a.asp file.

php?

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

We found that it redirects b.php.

It turns out that after executing redirect in asp, it will not be executed again. The following code.

After php executes the header, it 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 have to redirect After orientation, the following code cannot be executed:

Generally we use

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

but we can simply use the following method:

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

Also note that if Problems may also occur when encoding with Unicode (UTF-8), and you need to adjust the caching settings.

The above is the detailed content of Summary of php header('location: $url') page jump precautions. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!