1<?php
2
3session_start();
4
5 // check whether the loginbtn was clicked
6if (isset($_POST['loginbtn'])) {
7
8 include_once 'dbh.inc.php';
9
10 // initialize variable
11 $uid = trim($_POST['uid']);
12 $pwd = trim($_POST['pwd']);
13
14 // error handlers
15
16 // check if inputs are empty
17 if (empty($uid) || empty($pwd)) {
18
19 $conn = null;
20 header("Location: ../index.php?login=error_field");
21 exit();
22
23 } else {
24
25 // check if username is in database
26 $stmt = $conn->prepare("SELECT user_uid FROM users WHERE user_uid = ?");
27 $stmt->execute([$uid]);
28
29 if ($stmt->rowCount() < 1) {
30
31 $conn = null;
32 header("Location: ../index.php?login=error_username");
33 exit();
34
35 } else {
36
37 // check if password is correct
38 $stmt = $conn->prepare("SELECT user_pwd FROM users WHERE user_uid = ?");
39 $stmt->execute([$uid]);
40 $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
41
42 // dehashing the password
43 $hashedPwdCheck = password_verify($pwd, $result[0]['user_pwd']);
44
45 if ($hashedPwdCheck == false) {
46
47 $conn = null;
48 header("Location: ../index.php?login=error_password");
49 exit();
50
51 } else {
52
53 // login the user in
54 $stmt = $conn->prepare("SELECT user_id, user_first, user_last, user_email, user_uid FROM users WHERE user_uid = ?");
55 $stmt->execute([$uid]);
56 $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
57
58 $_SESSION['user_id'] = $result[0]['user_id'];
59 $_SESSION['user_first'] = $result[0]['user_first'];
60 $_SESSION['user_last'] = $result[0]['user_last'];
61 $_SESSION['user_email'] = $result[0]['user_email'];
62 $_SESSION['user_uid'] = $result[0]['user_uid'];
63
64 $conn = null;
65 header("Location: ../updates.php?login=success");
66 exit();
67
68 }
69
70 }
71
72 }
73
74} else {
75
76 header("Location: ../index.php?login=error");
77 exit();
78
79}
80
81?>
82