PHP provides functionality for redirecting pages after executing a function. To achieve this, follow these steps:
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.
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!