How can header() function be effectively utilized in PHP to redirect users to different pages based on certain conditions?
To redirect users to different pages based on certain conditions in PHP, the header() function can be effectively utilized. By setting the "Location" header to the desired URL, the user's browser will automatically redirect to the specified page. This can be useful for scenarios such as redirecting users after a successful login or based on certain criteria being met.
<?php
// Check a condition
if ($condition_met) {
header("Location: new_page.php");
exit;
} else {
header("Location: default_page.php");
exit;
}
?>
Keywords
Related Questions
- In PHP, what are some considerations for updating user passwords to a more secure hashing algorithm like SHA512 while maintaining usability and security for existing users?
- Are there alternative methods to restrict search results in PHP without relying on sessionid and cookies?
- What best practices should be followed when marking and deleting data records in PHP scripts?