How can the header() function in PHP be utilized for redirecting users after a successful login?
To redirect users after a successful login in PHP, the header() function can be utilized to send a raw HTTP header to the browser, which will redirect the user to the desired page. After validating the user's credentials, you can use the header() function with the "Location" parameter set to the URL of the page you want to redirect the user to.
// Validate user credentials
if ($valid_credentials) {
// Redirect user to dashboard page after successful login
header("Location: dashboard.php");
exit(); // Ensure that no other output is sent
} else {
// Display error message
echo "Invalid credentials. Please try again.";
}
Keywords
Related Questions
- What potential issues could arise when trying to upload files using PHP?
- What security considerations should be taken into account when implementing a file zipping and downloading feature with PHP?
- What steps can be taken to prevent data loss or truncation when transferring large amounts of data using PHP and cURL?