Home > Article > Backend Development > 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!