In PHP, we all know that the header() function can be used to redirect to other pages. Let us introduce in detail how to use the header() function in PHP to jump to another page.
Step 1: Create the source page
First, we need to create the source page we want to redirect. In our example, we will create a file called "source.php".
To create a simple HTML page, follow the steps below.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Source Page</title> </head> <body> <h1 id="Welcome-nbsp-to-nbsp-the-nbsp-Source-nbsp-Page">Welcome to the Source Page!</h1> <p>Click the following button to go to the destination page.</p> <button onclick="redirect()">Go to Destination Page</button> <script> function redirect() { window.location = "destination.php"; } </script> </body> </html>
This is a very simple HTML page, it contains a title and a button. When the button is clicked, the JavaScript function `redirect()` will be called to redirect the user to "destination.php".
Step 2: Create the target page
Next, we need to create the target page to which we want to redirect the user. In our example, we will create a file called "destination.php".
In this page, we will simply display a message to explain that the user has been successfully redirected.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Destination Page</title> </head> <body> <h1 id="Welcome-nbsp-to-nbsp-the-nbsp-Destination-nbsp-Page">Welcome to the Destination Page!</h1> <p>You have been successfully redirected from the Source Page.</p> </body> </html>
This is another very simple HTML page, it only contains a title and a message.
Step 3: Write PHP Code
Now we need to add the PHP code to the source page to show the user when they click the button on the source page Redirect to target page. In our example, we will use the header() function to achieve this.
<?php header('Location: destination.php'); exit; ?>
Among them, ‘ Location: ’ indicates the location we want to redirect to. In this case, we want to redirect the user to the "destination.php" page.
Note: You must use the exit statement before the header() function. Otherwise, all the content of the current page will be output before redirecting to the target page, causing problems.
Step 4: Test the Code
Now you can visit the source page in your browser and click the "Go to Destination Page" button to redirect the user Direct to target page.
Please note that you can also set the second parameter of the header() function to true to ensure accurate redirection.
<?php header('Location: destination.php', true, 301); exit; ?>
This way, you will be redirected to the target page with a 301 status code.
Conclusion:
It is very easy to implement page redirection using the header function in PHP. We can set the Location header to specify the page to jump to, and use exit to end the program directly after the code is executed.
The above is the detailed content of How to use header() function in PHP to jump to another page. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
