After a user logs in I am setting their $_SESSION['id'] to their id from the MySql database. This id auto increments so the first user to sign up will have the user id of 1.
Is this a secure way to handle this? Wouldn't an adversary be able to just set their $_SESSION['id'] to a random integer and potentially be logged in under someone else's account?
Here is my login.php file:
<?php
require_once 'includes/header.php';
if(isset($_POST['submit'])) {
// get the POST variables
$username = $_POST['username'];
$password = $_POST['password'];
// query the database
$statement = $db->prepare('SELECT * FROM users WHERE username=?');
$statement->execute(array($username));
if($user = $statement->fetch()) {
// compare password hashes
if(password_verify($password, $user['password'])) {
// successful authentication
$_SESSION['id']
}
}
}
?>