-
Notifications
You must be signed in to change notification settings - Fork 0
/
loginProcess.php
173 lines (163 loc) · 5.02 KB
/
loginProcess.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
session_start(); //start Session here because it can't start within the html tag!
$_SESSION['logged'] = false; //set flag to handle the session.
include ("header.php");
include ("includes/db_connection.php"); //connect to the database.
error_reporting(E_ERROR); //hide errors.
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/loginForm.css">
</head>
<body>
<?php
//Get values from form in login.php file.
$email = $_POST['email'];
$escapedPW = $_POST['pass'];
//Initialise variables for error handling.
$emailErr = $passErr = "";
//to prevent mysql injection
$email = stripslashes($email);
$escapedPW = stripslashes($escapedPW);
$email = mysqli_real_escape_string($conn,$email);
$escapedPW = mysqli_real_escape_string($conn,$escapedPW);
//query the database for email
$saltQuery="select SALT from users where EMAIL='$email'";
if ($conn->query($saltQuery)){
$result=$conn->query($saltQuery);
$row=$result->fetch_assoc();
$salt=$row['SALT'];
$saltedPW = $escapedPW . $salt;
$hashedPW = hash('sha256', $saltedPW);
//query the database for email and password.
$sql="select * from users where EMAIL = '$email' and PASS = '$hashedPW'";
if ($conn->query($sql)) {
$row=$conn->query($sql)->fetch_array();
}
}
else {
//if an error occurs regarding the query, show it.
echo "<h5>An error occured!</h5>";
}
if (empty($email)) {
$emailErr = "*Email is required";
} else {
$email = test_input($email);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format!";
}
}
if (empty($escapedPW)) {
$passErr = "*Password is required";
} else {
if (strlen( $escapedPW) < 8)
{
$passErr="Password should be at least 8 characters";
} else {
if ($row['PASS']!=$hashedPW)
{
$passErr="Wrong Password or Email.";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($emailErr!="" || $passErr!="")
{
?>
<h1>Sign in Form</h1>
<hr>
<form action="loginProcess.php" method="POST">
<table>
<tr>
<td>Email:</td>
<td>
<input type="text" id="email" name="email" placeholder="name@example.com" value="<?php if (isset($_COOKIE["email"])) { echo $_COOKIE["email"];} ?>" />
<span class="error"><?php echo $emailErr; ?></span>
</td>
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<input type="password" id="pass" name="pass" maxlength="20" placeholder="at least 8 characters" value="<?php if (isset($_COOKIE["pass"])) { echo $_COOKIE["pass"];} ?>" />
<span class="error"><?php echo $passErr; ?></span>
</td>
</tr>
<tr>
<td><input type="checkbox" name="remember" <?php if(isset($_COOKIE["email"])) { ?> checked <?php } ?>/></td>
<td>Remember me</td>
</tr>
<tr>
<td>
<input type="submit" id="btn" name="submit" value="Log in">
</td>
</tr>
</table>
</form>
<hr>
<?php
} else {
$_SESSION['logged']=true;
$_SESSION['email']=$email;
//to prevent mysql injection
$email = stripslashes($email);
$escapedPW = stripslashes($escapedPW);
$email = mysqli_real_escape_string($conn,$email);
$escapedPW = mysqli_real_escape_string($conn,$escapedPW);
//query the database for email
$saltQuery="select SALT from users where EMAIL='$email'";
if ($conn->query($saltQuery)){
$result=$conn->query($saltQuery);
$row=$result->fetch_assoc();
$salt=$row['SALT'];
$saltedPW = $escapedPW . $salt;
$hashedPW = hash('sha256', $saltedPW);
//query the database for email and hashed password.
$sql="select * from users where EMAIL = '$email' and PASS = '$hashedPW'";
if ($conn->query($sql)) {
$row=$conn->query($sql)->fetch_array();
}
}
else {
//if an error occurs regarding the query, show it.
echo "<h5>An error occured!</h5>";
}
if ($row['EMAIL'] == $email && $row['PASS']==$hashedPW) {
if (!empty($_POST["remember"])) //in case the user has checked Remember me box.
{
setcookie("email",$_POST["email"],time()+ (2592000)); //30 days remain remembered.
setcookie("pass", $_POST["pass"], time()+ (2592000));
}
else //if box not checked email and password fields will be emoty after logout.
{
if (isset($_COOKIE["email"]))
{
setcookie ("email", "");
}
if (isset($_COOKIE["pass"]))
{
setcookie ("pass", "");
}
}
header ("Location:welcome.php");
exit();
}
}
$conn->close();
?>
<div id="footer" align="center">
<?php
include("footer.php");
?>
</div>
</body>
</html>