setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Get the username and password from the form $username = $_POST['username']; $password = $_POST['password']; // Prepare the SQL statement to fetch the user $stmt = $connection->prepare("SELECT * FROM utenti WHERE user = :username"); $stmt->execute(['username' => $username]); $user = $stmt->fetch(PDO::FETCH_ASSOC); // Verify the password if ($user && $user['password'] === $password) { // Password is correct, start a new session $_SESSION['user_id'] = $user['idut']; $_SESSION['username'] = $user['user']; $_SESSION['livello'] = $user['livello']; $_SESSION["login"] = true; // Redirect to a different page after successful login header("Location: index.php"); exit(); } else { // Invalid credentials $error = "Invalid username or password."; } } catch (PDOException $e) { $error = "Error: " . $e->getMessage(); } } ?>