-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
146 lines (130 loc) · 5.09 KB
/
login.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
<?php
ini_set('display_errors',1);
ini_set('log_errors',1);
ini_set('error_log',dirname(__FILE__).'/log.txt');
error_reporting(E_ALL);
session_start();
require_once "pdo.php";
$salt = 'XyZzy12*_';
$stored_hash = 'ac7cc39c493be9ccbd7548984d90185e';
//This if statement won't get executed on opening this webpage the first time.It runs when the login button is pressed.
if (isset($_POST['username']) && isset($_POST['password'])) {
if( strlen($_POST['password']) < 1 && strlen($_POST['username']) < 1 ){
$_SESSION['error'] = "Username and Password is required";
header("Location: login.php");
return;
}
else if (strlen($_POST['username']) < 1 ) {
$_SESSION["password"]= $_POST["password"];
$_SESSION['error'] = "Username is required";
header("Location: login.php");
return;
}
else if( strlen($_POST['password']) < 1 ){
$_SESSION["username"]= $_POST["username"];
$_SESSION['error'] = "Password is required";
header("Location: login.php");
return;
}
else {
//The username and password entered by the user will be stored in session even before validating them.
//So that it can be displayed for them to correct even if it is wrong.
$_SESSION["username"]= $_POST["username"];
$_SESSION["password"]= $_POST["password"];
//For value
$_SESSION["usernamevalue"]= $_POST["username"];
$_SESSION["passwordvalue"]= $_POST["password"];
$stmt = $pdo->prepare("SELECT username,password,account_id,darkmode,score FROM accounts WHERE username= :username");
$stmt -> execute(array(":username" => $_POST['username']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($row !== false){
$RetrievedPasswordHash = $row["password"];
$check = hash('md5', $salt.$_POST['password']);
if ($check == $RetrievedPasswordHash) {
$_SESSION['account_id'] = $row['account_id'];
$_SESSION['darkmode'] = $row['darkmode'];
$_SESSION['score'] = $row['score'];
error_log("Login success ".$_POST['username']);
$_SESSION['username'] = htmlentities($_POST['username']);
header("Location: home.php");
return;
}
else {
$pass = $_POST['password'];
$_SESSION['error'] = "Sorry, your password was incorrect. Please double-check your password.";
error_log(" || Login fail || account name =".$_POST['username']." || Password="." $pass"." || ");
header("Location: login.php");
return;
}
}
else{
$_SESSION["error"] = "The username you entered doesn't belong to an account. Please check your username and try again.";
header("Location: login.php");
return;
}
}
}
if(!isset($_SESSION['usernamevalue'])){
$_SESSION["usernamevalue"]= "";
}
if(!isset($_SESSION['passwordvalue'])){
$_SESSION["passwordvalue"]= "";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login</title>
<link href="assets/img/favicon.png" rel="icon">
<link href="https://cdnjs.cloudflare.com/ajax/libs/simple-line-icons/2.4.1/css/simple-line-icons.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">
<link rel="stylesheet" href="css/login.css">
<style>
#login{
text-align:center;
margin:40px;
}
</style>
</head>
<body>
<div class="registration-form">
<form method="post">
<div id="login">
<h1>LOGIN</h1>
</div>
<div class="form-group">
<input type="text" class="form-control item" id="username" name="username" placeholder="USERNAME" value="<?= $_SESSION['usernamevalue']?>">
</div>
<div class="form-group ">
<input type="password" class="form-control item passwordclass" id="password" name="password" placeholder="PASSWORD" value="<?= $_SESSION['passwordvalue']?>"><i class="far fa-eye" id="togglePassword"></i>
</div>
<div class="form-group">
<button type="submit" class="btn btn-block create-account">LOG IN</button>
</div>
<div class="form-group">
<?php
//The session "username" and "password" will be unset only on page reload.
//page reload will happen if you reload it or If your input to the form is correct(and you will be reloaded to another page.)
unset($_SESSION["username"]);
unset($_SESSION["password"]);
if (isset($_SESSION['error'])) {
echo '<p style="color: red;text-align:center;">'.$_SESSION['error']."</p>\n";
unset($_SESSION['error']);
}
?>
</div>
</form>
<div class="social-media" id="linkdiv">
<h5>DON'T HAVE AN ACCOUNT?<a id="signuplink" href="signup.php">SIGN UP</a></h5>
</div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.min.js"></script>
<script src="js/login.js"></script>
<script src="js/togglepassword.js"></script>
</body>
</html>