-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication.inc
108 lines (84 loc) · 2.79 KB
/
authentication.inc
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
<?php
function make_safe($inputText){
$inputText=strip_tags($inputText);
$inputText=str_replace(" ", "", $inputText);
return $inputText;
}
function randHash($len=32)
{
return substr(md5(openssl_random_pseudo_bytes(20)),-$len);
}
function authenticateUser($connection, $username, $password)
{
// Test the username and password parameters
if (!isset($username) || !isset($password))
return false;
// Formulate the SQL find the user
$query = "SELECT * FROM user WHERE username = '$username'
AND password = '$password'";
// Execute the query
$result = $connection->query($query);
//showerror();
//if (!$result = @ mysql_query ($query, $connection))
// exactly one row? then we have found the user
if ($result->num_rows != 1){
return false;}
else{
$loginkey = randHash(20);
$updatekeyquery = "UPDATE user SET loginkey = '$loginkey' WHERE username = '$username'";
$connection->query($updatekeyquery);
setcookie('loginkeycookie', $loginkey, time() + (86400 * 1), "/"); // 86400 = 1 day
?>
<html>
<body>
<?php
return true;
}
}
// Connects to a session and checks that the user has
// authenticated and that the remote IP address matches
// the address used to create the session.
// also checks for concurrency
function sessionAuthenticated()
{
include('sqlconnection.php');
if(session_status()!=2)
session_start();
// Check if the user hasn't logged in
if (!isset($_SESSION["loginUsername"]))
{
// The request does not identify a session
$_SESSION["message"] = "You are not authorized to access the URL
{$_SERVER["REQUEST_URI"]}";
return false;
}
// Check if the request is from a different IP address to previously
if (!isset($_SESSION["loginIP"]) ||
($_SESSION["loginIP"] != $_SERVER["REMOTE_ADDR"]))
{
// The request did not originate from the machine
// that was used to create the session.
// THIS IS POSSIBLY A SESSION HIJACK ATTEMPT
$_SESSION["message"] = "You are not authorized to access the URL
{$_SERVER["REQUEST_URI"]} from the address
{$_SERVER["REMOTE_ADDR"]}";
return false;
}
// for concurrency check if cookie is same as latest on database
$username = $_SESSION["loginUsername"];
$query = "SELECT * FROM user WHERE username = '$username'";
$result = mysqli_query($conn,$query);
$row=mysqli_fetch_assoc($result);
if(!isset($_COOKIE['loginkeycookie']) || $_COOKIE['loginkeycookie'] != $row['loginkey']){
$_SESSION["logingmsg"]= "You are logged in elsewhere, login again to use on this machine";
return false;
}
return true;
}
//if session is not verified then direct to index.
function verifyUserSession(){
if (!sessionAuthenticated()) {
header("location: logout.php");
}
}
?>