-
Notifications
You must be signed in to change notification settings - Fork 141
/
authenticate.php
71 lines (51 loc) · 1.72 KB
/
authenticate.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
<?php
require_once './config/config.php';
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = filter_input(INPUT_POST, 'username');
$passwd = filter_input(INPUT_POST, 'passwd');
$remember = filter_input(INPUT_POST, 'remember');
//echo password_verify('admin', '$2y$10$RnDwpen5c8.gtZLaxHEHDOKWY77t/20A4RRkWBsjlPuu7Wmy0HyBu'); exit;
//Get DB instance.
$db = getDbInstance();
$db->where("user_name", $username);
$row = $db->get('admin_accounts');
if ($db->count >= 1) {
$db_password = $row[0]['password'];
$user_id = $row[0]['id'];
if (password_verify($passwd, $db_password)) {
$_SESSION['user_logged_in'] = TRUE;
$_SESSION['admin_type'] = $row[0]['admin_type'];
if ($remember) {
$series_id = randomString(16);
$remember_token = getSecureRandomToken(20);
$encryted_remember_token = password_hash($remember_token,PASSWORD_DEFAULT);
$expiry_time = date('Y-m-d H:i:s', strtotime(' + 30 days'));
$expires = strtotime($expiry_time);
setcookie('series_id', $series_id, $expires, "/");
setcookie('remember_token', $remember_token, $expires, "/");
$db = getDbInstance();
$db->where ('id',$user_id);
$update_remember = array(
'series_id'=> $series_id,
'remember_token' => $encryted_remember_token,
'expires' =>$expiry_time
);
$db->update("admin_accounts", $update_remember);
}
//Authentication successfull redirect user
header('Location:index.php');
} else {
$_SESSION['login_failure'] = "Invalid user name or password";
header('Location:login.php');
}
exit;
} else {
$_SESSION['login_failure'] = "Invalid user name or password";
header('Location:login.php');
exit;
}
}
else {
die('Method Not allowed');
}