<?php

    // Step 1: start the session

    session_name('chat_hw10');
    session_start();

    // Step 2: connect to the database

    include("connect.php");

    // Step 3: Check if a user with the attempted login info  exists
    $check_for_user_query = $db->prepare("
        SELECT COUNT(`username`) FROM `users`
        WHERE
            `username` = :username
        AND
            `password` = :password
    ");

    $check_for_user_query->execute(
        array(
            ':username' => $_POST['username'],
            ':password' => $_POST['password']
        )
    );

    if ($check_for_user_query->fetchColumn(0) == 0)
    {
        die("Invalid login information!");
    }
        
    // Step 4: Store that this user logged in successfully in the session
    // array for later

    $_SESSION['username'] = $_POST['username'];

    header("Location: chat.php");

?>
