Home  >  Article  >  Backend Development  >  How to use header() function in PHP to jump to another page

How to use header() function in PHP to jump to another page

PHPz
PHPzOriginal
2023-03-21 15:49:491724browse

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>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>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(&#39;Location: destination.php&#39;);
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(&#39;Location: destination.php&#39;, 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!

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