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
DDDOriginal
2024-11-25 22:26:12905browse

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");

    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();

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();
}

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!

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