-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
238 lines (188 loc) · 7.49 KB
/
index.php
File metadata and controls
238 lines (188 loc) · 7.49 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php session_start(); ?>
<?php require_once('inc/connection.php'); ?>
<?php require_once('inc/func.php'); ?>
<?php require_once('auth/registration.php'); ?>
<?php
//if already logged users cannot access this page
if (isset($_SESSION['id'])) {
//echo("<script>location.href = '".ADMIN_URL."/index.php?msg=$msg';</script>");
header('Location: users/users.php?msg=alreadyLogged');
}
// check for form login
if (isset($_POST['signinBtn'])) {
$errors = array();
// check if the username and password has been entered
//if empty or enter a space
$email = mysqli_real_escape_string($connection, $_POST['email']);
$password = mysqli_real_escape_string($connection, $_POST['password']);
//echo $email;
//echo $password;
//die();
$req_fields = array('email', 'password');
$errors = array_merge($errors, check_req_fields($req_fields));
// check if there are no any errors in the form
if (empty($errors)) {
// save username and password into variables
$email = mysqli_real_escape_string($connection, $_POST['email']);
$password = mysqli_real_escape_string($connection, $_POST['password']);
$hashed_password = sha1($password);
// prepare database query
$query = "SELECT * FROM user
WHERE email = '{$email}'
AND password = '{$hashed_password}'
LIMIT 1";
$result_set = mysqli_query($connection, $query);
verify_query($result_set); {
// query succesfful
if (mysqli_num_rows($result_set) == 1) {
// valid user found
$user = mysqli_fetch_assoc($result_set);
$_SESSION['id'] = $user['id'];
$_SESSION['first_name'] = $user['first_name'];
//updating last login
$query = "UPDATE user SET last_login=NOW()";
$query .= "WHERE id={$_SESSION['id']} LIMIT 1";
$result_set = mysqli_query($connection, $query);
verify_query($result_set);
// redirect to users.php
header('Location: users/users.php');
} else {
// user name and password invalid
$errors[] = 'Invalid Username / Password';
}
}
}
}
//chk for sign up
if (isset($_POST['signupBtn'])) {
$errors2 = array();
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$password = $_POST['password'];
// checking required fields
$req_fields = array('first_name', 'last_name', 'email', 'password');
$errors2 = array_merge($errors2, check_req_fields($req_fields));
// checking max length
$max_len_fields = array('first_name' => 50, 'last_name' => 100, 'email' => 100, 'password' => 40);
$errors2 = array_merge($errors2, check_max_len($max_len_fields));
// checking email address
if (!is_email($_POST['email'])) {
$errors2[] = 'Email address is invalid.';
}
//chk email add already exists
$email = mysqli_real_escape_string($connection, $_POST['email']);
$query = "SELECT * FROM user WHERE email='{$email}' LIMIT 1";
$result_set = mysqli_query($connection, $query);
verify_query($result_set);
// query succesfful
if (mysqli_num_rows($result_set) == 1) {
$errors2[] = 'email address already exists.';
}
if (empty($errors2)) {
registerUser($first_name, $last_name, $email, $password);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Log In - User Management System</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>Log In - User Management System</title>
<link rel="stylesheet" href="css/login.css">
<link rel="stylesheet" href="css/css/all.min.css">
<link rel="stylesheet" type="text/css" href="css/alert.css">
<script src="js/alert.js"></script>
</head>
<body>
<?php
if (isset($errors) && !empty($errors)) {
//echo '<p class="error">Invalid Username / Password</p>';
// echo '<p class="error">' . $errors[0] . '</p>';
$error = ucfirst(str_replace("_", " ", $errors[0]));
echo "<script>swal('Oops... ', '$error', 'warning');</script>";
}
if (isset($errors2) && !empty($errors2)) {
//echo '<p class="error">Invalid Username / Password</p>';
$error = ucfirst(str_replace("_", " ", $errors2[0]));
echo "<script>
swal('Oops.. There were error(s) on your signup form', '$error', 'warning')
.then((value) => {
container.classList.add('right-panel-active');
});;
</script>";
}
if (isset($_GET['logout'])) {
echo '<p class="info">You have successfully log out from the system.</p>';
}
if (isset($_GET['verify'])) {
//check for the verification mail send or not
if ($_GET['verify'] == 'Email sent'){
echo "<script>swal('Verification link has been sent to your email', 'Please check your email!', 'info');</script>";
}
if ($_GET['verify'] == 'Email send failed'){
echo "<script>swal('Oops.. mail could not be sent !', 'Please input a correct email or try again later', 'error');</script>";
}
//check for the verification link is correct
if ($_GET['verify'] == 'Email verified successfully'){
// echo '<p class="info">You have verified your email successfully. Now you can logging to the system.</p>';
echo "<script>swal('You have verified your email successfully!', 'Now you can logging to the system', 'success');</script>";
}
if ($_GET['verify'] == 'Invalid verification code'){
echo "<script>swal('Oops.. Invalid verification link!', 'Please check the email again', 'warning');</script>";
}
}
?>
<div class="container" id="container">
<div class="form-container sign-up-container">
<form action="index.php" method="post">
<h1>Create Account</h1>
<div class="social-container">
<a href="#" class="social"><i class="fab fa-facebook-f"></i></a>
<a href="#" class="social"><i class="fab fa-google-plus-g"></i></a>
<a href="#" class="social"><i class="fab fa-linkedin-in"></i></a>
</div>
<span>or use your email for registration</span>
<input type="text" placeholder="First Name" name="first_name" />
<input type="text" placeholder="Last Name" name="last_name"/>
<input type="email" placeholder="Email" name="email"/>
<input type="password" placeholder="Password" name="password"/>
<button name="signupBtn">Sign Up</button>
</form>
</div>
<div class="form-container sign-in-container">
<form action="index.php" method="post">
<h1>Sign in</h1>
<div class="social-container">
<a href="#" class="social"><i class="fab fa-facebook-f"></i></a>
<a href="#" class="social"><i class="fab fa-google-plus-g"></i></a>
<a href="#" class="social"><i class="fab fa-linkedin-in"></i></a>
</div>
<span>or use your account</span>
<input type="email" placeholder="Email" name="email"/>
<input type="password" placeholder="Password" name="password"/>
<a href="#">Forgot your password?</a>
<button name="signinBtn">Sign In</button>
</form>
</div>
<div class="overlay-container">
<div class="overlay">
<div class="overlay-panel overlay-left">
<h1>Welcome Back!</h1>
<p>To keep connected with us please login with your personal info</p>
<button class="ghost" id="signIn">Sign In</button>
</div>
<div class="overlay-panel overlay-right">
<h1>Hello, Friend!</h1>
<p>Enter your personal details and start journey with us</p>
<button class="ghost" id="signUp">Sign Up</button>
</div>
</div>
</div>
</div>
<script src="js/login.js"></script>
</body>
</html>
<?php mysqli_close($connection); ?>