Is it considered best practice to use header redirection in PHP to redirect users to another page after login?
Using header redirection in PHP to redirect users to another page after login is considered a common and effective practice. This method helps maintain clean URLs and improves security by preventing users from accessing restricted pages directly. To implement this, you can use the header() function in PHP to send a raw HTTP header to the browser, instructing it to redirect to the desired page.
<?php
// Start the session
session_start();
// Check if the user is logged in
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
// Redirect to the desired page
header('Location: dashboard.php');
exit();
} else {
// Redirect to the login page if the user is not logged in
header('Location: login.php');
exit();
}
?>
Keywords
Related Questions
- What are the common pitfalls when managing PHP versions in Apache2 and CLI?
- Are there any specific PHP libraries or resources that can assist in time calculations and conversions?
- How can PHP developers balance their willingness to help others with the need to prioritize their own projects and time effectively?