-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuth.php
90 lines (59 loc) · 1.98 KB
/
Auth.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
<?php
require_once('Log.php');
require_once('Input.php');
class Auth
{
public static $password = '$2y$10$SLjwBwdOVvnMgWxvTI4Gb.YVcmDlPTpQystHMO2Kfyi/DS8rgA0Fm';
public static $username="";
// attempt will take in a $username and $password. If the $username is guest and the $password matches the hashed password above then set the LOGGED_IN_USER session variable as before. Use the Log class to log an info message: "User $username logged in.". If either the username or password are incorrect then log an error: "User $username failed to log in!". You will need to use the PHP method password_verify() to check the password hash.
public static function attempt($username, $password)
{
$logObject=new Log();
if ($username=='guest'&&password_verify($password,self::$password)) {
$message=$username . "has successfully logged in.";
$logObject->logInfo($message);
$_SESSION['user_is_logged_in']=true;
$_SESSION['username']=$_POST['username'];
return true;
}else{
$message=$username . "has failed to log in.";
$logObject->logError($message);
return false;
}
}
// will check to see if user is logged in
public static function check(){
if (isset($_SESSION['username'])) {
return true;
}else{
return false;
}
}
// will return username
public static function user(){
if (self::check()) {
return $_SESSION['username'];
}else{
return null;
}
}
// will logout current session
public static function logout(){
// clear $_SESSION array
session_unset();
// delete session data on the server
session_destroy();
// ensure client is sent a new session cookie
session_regenerate_id();
// start a new session - session_destroy() ended our previous session so
// if we want to store any new data in $_SESSION we must start a new one
session_start();
header("Location: login.php");
die();
}
// This function redirects user to new page
public static function redirect($url){
header($url);
die();
}
}