Home > Backend Development > PHP Tutorial > How Can I Redirect a PHP Page After Function Execution?

How Can I Redirect a PHP Page After Function Execution?

DDD
Release: 2024-11-25 22:26:12
Original
883 people have browsed it

How Can I Redirect a PHP Page After Function Execution?

PHP Page Redirection After Function Execution

PHP provides functionality for redirecting pages after executing a function. To achieve this, follow these steps:

  1. Use the header function:

    header("Location: http://www.yourwebsite.com/user.php");
    Copy after login

    This will send a header to the browser to redirect to the specified URL.

  2. Call exit():

    After calling header, it's good practice to immediately call exit() to prevent subsequent code from executing and potentially causing errors.

    header("Location: http://www.yourwebsite.com/user.php");
    exit();
    Copy after login

Example:

if (...) {
    // I am using echo here.
} else if ($_SESSION['qnum'] > 10) {
    session_destroy();
    echo "Some error occurred.";

    // Redirect to "user.php".
    header("Location: user.php");
    exit();
}
Copy after login

Important Note:

Remember to call header() before sending any other output, including HTML tags and blank lines. Sending output before calling header() can result in errors.

The above is the detailed content of How Can I Redirect a PHP Page After Function Execution?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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