When developing a login script in PHP, what are the best practices for handling user IDs?
When developing a login script in PHP, it is best practice to store user IDs securely to prevent unauthorized access. One way to achieve this is by using sessions to store the user ID after a successful login and verifying it on subsequent pages to ensure the user is authenticated.
// Start the session
session_start();
// Set the user ID in the session after successful login
$_SESSION['user_id'] = $user_id;
// Verify the user ID on subsequent pages to ensure authentication
if(isset($_SESSION['user_id'])) {
// User is authenticated
} else {
// Redirect to login page
header("Location: login.php");
exit();
}