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

How Can I Redirect a PHP Page After a Function Executes?

Susan Sarandon
Susan SarandonOriginal
2024-11-27 22:38:14566browse

How Can I Redirect a PHP Page After a Function Executes?

PHP Page Redirection After Executing a Function

Can PHP redirect to a different page after completing a specific function? Yes, you can use the header function to achieve this.

To redirect to a page called "user.php" located in the same root folder, you would use the following code:

header("Location: user.php");
exit();

It is important to call exit() after the header function to prevent any code below it from executing.

Here's an example of how you might use this in the code you provided:

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

Note: Ensure that there is no output before the header function is called, as this could break the redirect. Additionally, verify that the code is executed before any other output is sent to the browser.

The above is the detailed content of How Can I Redirect a PHP Page After a Function Executes?. 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