Home  >  Article  >  Backend Development  >  How to redirect web pages in PHP

How to redirect web pages in PHP

coldplay.xixi
coldplay.xixiOriginal
2020-09-01 13:47:272703browse

How to redirect web pages in PHP: 1. Use the [header()] function to redirect; 2. Use the meta tag in the HTML header; 3. Use javascript to jump.

How to redirect web pages in PHP

[Related learning recommendations: php graphic tutorial]

How to redirect web pages in PHP:

hhw: Use the first method to redirect: http://127.0.0.1/tp5 Simplified to http://127.0.0.1, that is, write the index.php file in the www directory to the php code in the first type:

<?php
header(&#39;content-type:text/html;charset=uft-8&#39;);
header(&#39;location:tp5/index.php&#39;);
?>

or directly:

<?php
header(&#39;content-type:text/html;charset=uft-8&#39;);
header(&#39;location:tp5/public/index.php&#39;);
?>

The first one: Use the header() function for redirection, which is what I use most. (Note! There cannot be a space between locationhe and ":", otherwise it will have no effect!)

<?php
header(&#39;content-type:text/html;charset=uft-8);
//重定向页面
header(&#39;location:index.php&#39;);
?>

Second: Use the meta tag in the HTML header to define http-equiv=refresh and content="jump The time it takes to transfer (in seconds); url = jump address”

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
//跳转页面,跳转时间:3秒钟,目标地址:index.php
<meta http-equiv="refresh" content="3;url=index.php">
<title>skip...</title>
</head>

or

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
$url=&#39;index.php&#39;;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
//跳转页面,跳转时间:2秒钟,目标地址:index.php
<meta http-equiv="refresh" content="2;url=<?php echo $url; ?>">
<title>skip...</title>
</head>

The third way: use javascript to jump

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
$url=&#39;index.php&#39;;
//立即跳转至目标页面
echo <script>window.location.href=&#39;$url&#39;;</script>;
?>

Relevant learning recommendations: php programming (video)

The above is the detailed content of How to redirect web pages in PHP. 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