What are some potential solutions to prevent multiple users from logging in with the same credentials in PHP?
To prevent multiple users from logging in with the same credentials in PHP, we can implement a session-based solution where a unique session ID is generated for each user upon successful login. This session ID can be stored in a database along with the user's credentials, and checked against when a user attempts to log in. If a session ID is already associated with a user's credentials, it would prevent another user from logging in with the same credentials.
// Check if the user's credentials are valid
if ($username == $storedUsername && $password == $storedPassword) {
// Generate a unique session ID
$sessionId = uniqid();
// Store the session ID in the database
$query = "UPDATE users SET session_id = '$sessionId' WHERE username = '$username'";
// Execute the query
// Set the session ID in the user's session
$_SESSION['session_id'] = $sessionId;
// Redirect the user to the home page
header("Location: home.php");
} else {
// Invalid credentials, display an error message
echo "Invalid username or password.";
}