Preface
There are many ways to implement page jumps in PHP. Common methods include the header() function and HTML elements that come with PHP.<meta>
Tags and JavaScript
The difference table of the three is as follows:
Jump method | advantage | shortcoming | Applicable scenarios |
---|---|---|---|
PHP header() | Server-side jump, suitable for handling redirects, supports HTTP status code | Must be called before output, no HTML output | Used for redirection of PHP backend, processing login and other permission jumps |
HTML <meta>
|
Simple and easy to use, suitable for automatic jump of static pages | The jump time is inflexible and the user behavior cannot be controlled | Timed jump, suitable for simple automatic jump scenarios |
JavaScript | Strong flexibility, supports client operation | Need to support JS on the client, it may be disabled | Suitable for front-end interactions, such as countdown or jump after user operations |
The basic choices are as follows:
-
PHP header()
: Suitable for backend page redirection, often used for jump after user authentication or background logic judgment -
HTML <meta>
: Suitable for simple page redirection, often used for automatic redirection after static pages or announcement prompts -
JavaScript
: Suitable for front-end page redirection, with higher flexibility, suitable for scenarios where users need to interact
1. PHP Jump
The header() function is used to send original HTTP header information to the browser, which can realize page redirection.
When using it, you must make sure that nothing is output before calling the header() function (i.e., no HTML or echo output), otherwise the header information will not be sent
<?php // PHP page jumpheader('Location: /weixin_47872288'); // Jump to the specified URL// orheader('location:'); exit; // End the current script to prevent subsequent code execution?>
The points to note are as follows:
- The header() function must be called before any HTML content or blank output
- You can use HTTP status codes to specify jump types, such as header('Location: URL', true, 301) specified as permanent redirect
2. HTML jump
In HTML, you can use<meta>
Tags realize automatic page jump
Usually placed in the tag, by settinghttp-equiv="refresh"
Attributes and content attributes to realize the timed page jump
The basic demo is as follows:
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta http-equiv="refresh" content="3;url="> <title>Page jump</title> </head> <body> <p>The page will be in 3 Automatically jump to the new one after seconds URL。</p> </body> </html>
This method is suitable for automatic page jump, often used for prompts or delay jumps, and the delay time can be adjusted according to needs
3. JavaScript jump
JavaScript can use objects to realize page jump
It is an object of the browser. By modifying its properties, the browser can jump to the specified URL
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>JavaScript Page jump</title> <script type="text/javascript"> setTimeout(function() { = ""; // Jump to the specified URL }, 3000); // Delay 3 seconds </script> </head> <body> <p>The page will be in 3 Automatically jump in seconds。</p> </body> </html>
Jump can be used in client scripts and supports more flexible control, such as jump condition judgment, etc.