I’m trying to create a login function using
But, when I submit, I get a few errors
why are the session variables not set?
on
<?php
include '../db/pdo_conn.php';
echo '<pre>';print_r($_POST);echo '</pre>';
require 'config.php';
$data = []; // array to hold a trimmed working copy of the form data. will also receive the initial data when editing/updating existing data
$errors = []; // array to hold user/validation errors
// post method form processing
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$data = array_map('trim',$_POST);
echo '<pre>';print_r($data);echo '</pre>';
if($data['email'] === '')
{
$errors['email'] = "Email is required";
}
else if(!filter_var($data['email'],FILTER_VALIDATE_EMAIL))
{
$errors['email'] = "Email must be in correct format.";
}
if($data['password'] === '')
{
$errors['password'] = "Password is required";
}
echo '<pre>';print_r($errors);echo '</pre>';
if (empty($errors)) { // Input is correct, see if user exists
$sql = "SELECT * FROM users WHERE email=?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$data['email']]);
echo $sql;
if($result = $stmt->fetch())
{
if(!password_verify($data['password'], $result['password']))
{
$errors['wrong'] = 'Incorrect email/password';
}
else
{
$_SESSION['name'] = $result['first_name']. ' ' . $result['last_name'];
$_SESSION['email'] = $result['email'];
$_SESSION['role'] = $result['role'];
$_SESSION['success_message'] = 'You have logged in.';
echo '<pre>';print_r($_SESSION);echo '</pre>';
}
}
}
}
?>
My database has