<?php
session_start
();
session_unset(); //Clean everything out of the session.

require('DbPdoConnection.php');

if (!isset(
$_POST['email']) || !isset($_POST['pw'])) {
    
errorAndExit("No username or password provided");
}

$db DbPdoConnection::getDbConnection();

$sql "SELECT email, password FROM users WHERE email = '{$_POST['email']}'";
$results $db->query($sql);

// DEBUG
if (!$results) {
    
var_dump($db->errorInfo());
    
var_dump($sql);
    die;
}

$rows $results->fetchAll();

if (empty(
$rows)) {
    
errorAndExit("User not found.");
}

if (
$rows[0]['password'] != $_POST['pw']) {
    
errorAndExit("Bad password.");
}

$_SESSION['user'] = $rows[0];
$_SESSION['success'][] = "You successfully logged in.";
header('Location: /');
exit();


function 
errorAndExit($msg) {
    
$_SESSION['error'][] = $msg;

    
header('Location: /login2');
    exit();
}