Home >Backend Development >PHP Tutorial >Summary of php header('location: $url') page jump precautions

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

伊谢尔伦
伊谢尔伦Original
2017-06-23 14:06:337495browse

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");

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>

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

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

This is the only way:

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

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>

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>

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
{
...
}

but we can simply use the following method:

if(...)
{header("...");exit();}

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!

Statement:
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