Home  >  Article  >  Backend Development  >  How to Redirect a PHP Page After Function Execution?

How to Redirect a PHP Page After Function Execution?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-23 13:28:14744browse

How to Redirect a PHP Page After Function Execution?

PHP Page Redirection After Function Execution

In PHP, it is possible to redirect a page after executing a function. To achieve this, use the header() function.

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

Call header() before sending any output, including echo statements or blank lines. Failure to do so may result in an error.

Once header() is called, it is considered best practice to invoke exit() to prevent any subsequent code from being executed.

For instance, in your provided code:

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

You should modify it to:

if (...) {
    // I am using echo here.
} else if ($_SESSION['qnum'] > 10) { 
    session_destroy();
    header("Location: user.php");
    exit();
}

By employing these techniques, you can effectively redirect a page after executing a PHP function.

The above is the detailed content of How to 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