How can a delay of 1 or 2 seconds be implemented for automatic redirection after login in PHP?
To implement a delay of 1 or 2 seconds for automatic redirection after login in PHP, you can use the `header()` function along with the `sleep()` function. By setting a sleep time before redirecting the user, you can create a delay in the redirection process.
<?php
// Start the session
session_start();
// Check if user is logged in
if(isset($_SESSION['user_id'])) {
// Delay redirection by 1 or 2 seconds
sleep(1); // 1 second delay
// sleep(2); // 2 second delay
// Redirect user to the desired page
header('Location: dashboard.php');
exit();
} else {
// Redirect user to login page if not logged in
header('Location: login.php');
exit();
}
?>
Keywords
Related Questions
- How can the use of arrays in PHP be a more efficient alternative to switch-case statements for certain scenarios?
- What are the limitations of using HTTP for server-side browser monitoring in PHP applications?
- What are the potential risks or challenges associated with replacing the PHPBB logo with a custom logo for a forum?