-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathauthenticate.php
71 lines (61 loc) · 1.73 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');
$password = filter_input(INPUT_POST, 'password');
$remember = filter_input(INPUT_POST, 'remember');
// Get DB instance.
$db = getDbInstance();
$db->where('user_name', $username);
$row = $db->getOne('users');
if ($db->count >= 1)
{
$db_password = $row['password'];
$user_id = $row['id'];
if (password_verify($password, $db_password))
{
$_SESSION['user_logged_in'] = TRUE;
$_SESSION['admin_type'] = $row['admin_type'];
$_SESSION['user_id'] = $row['id'];
$_SESSION['user_name'] = $row['user_name'];
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('users', $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');
}