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

How to Redirect a PHP Page After Function Execution?

Mary-Kate Olsen
Release: 2024-11-23 13:28:14
Original
813 people have browsed it

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();
Copy after login

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".
}
Copy after login

You should modify it to:

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

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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template