How to implement page redirection in PHP? (Pictures + Videos)

藏色散人
Release: 2018-09-27 14:22:05
Original
9250 people have browsed it

This article mainly introduces to you the detailed explanation of the three methods of PHP to implement page redirection.

So what is page redirection?

In the process of website development, when we encounter web page migration or website adjustment, we need to make a redirection to prevent traffic loss. Page redirection is to redirect various network requests to other locations through various methods.

Below we will introduce to you the specific method of implementing page redirection through simple code examples.

Method 1: header redirection

<?php
$url = "http://php.cn";
if (isset($url)) {
 header("Location:$url");
} else {
 echo "没有跳转的地址!";
}
Copy after login

Here we define a $url variable, which represents the url address to be redirected to. Then use if to determine whether the redirect link exists. If it exists, jump to the new address "http://php.cn". If not, output "No jump address!".

If no jump link is defined, the return value is as follows:

How to implement page redirection in PHP? (Pictures + Videos)

If a new url is defined, it is normal The jump is as follows:

How to implement page redirection in PHP? (Pictures + Videos)

Note : The header() function sends the original HTTP header to the client. The parameter is the new url address.

Method 2: js script redirection

<?php
$url = "http://php.cn";
if (isset($url)) {
echo "<SCRIPT language= &#39;JavaScript&#39;>location.href=&#39;$url&#39;</SCRIPT>";
} else {
 echo "没有跳转的地址!";
}
Copy after login

Also first determine whether the link exists, and then here we mainly use location.href in js, that is It means jump.

Method three: html tag redirection

<?php
$url = "http://php.cn";
if (!isset($url)) {
    exit("没有跳转的地址!");
}
?>
<HTML>
<head>
    <meta HTTP-EQUIV="REFRESH" CONTENT="3; URL=&#39;<?php echo $url; ?>&#39; ">
</head>
<body>
</body>
Copy after login

Similarly, we first determine whether there is a jump link, and then mainly use

In the tag, REFRESH means defining a refresh, 3 is the refresh time, the unit is seconds, and the parameter in the URL is the refreshed file, that is, the new jump link address.

This article is a detailed introduction to the three methods of implementing page redirection in PHP. I hope it will be helpful to friends in need!

If you want to learn more about PHP, you can follow the PHP Chinese website PHP Video Tutorial. Everyone is welcome to learn and refer to it.

The above is the detailed content of How to implement page redirection in PHP? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
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!