-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsession.php
90 lines (63 loc) · 1.79 KB
/
session.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
session_start();
class Session {
public $user;
public $hash;
public $id;
public $hasLoggedIn = false;
public function generateHash() {
//$this->hash = uniqid("", TRUE);
$this->hash = hash("sha256", uniqid("", TRUE));
}
public function setID($i) {
$this->id = $i;
}
public function setUser($u) {
$this->user = $u;
}
public function setHash($h) {
$this->hash = $h;
}
public function setPHPSession() {
if(!isset($this->id) || !isset($this->hash)) {
return false;
}
$_SESSION['id'] = $this->id;
$_SESSION['hash'] = $this->hash;
return true;
}
public function restorePHPSession() {
if(isset($_SESSION['id']) && isset($_SESSION['hash'])) {
$this->id = $_SESSION['id'];
$this->hash = $_SESSION['hash'];
return true;
} else {
return false;
}
}
public function destroyPHPSession() {
$_SESSION['id'] = null;
$_SESSION['hash'] = null;
return true;
}
public function isLoggedIn() {
$this->restorePHPSession();
global $mysql;
$sql = "SELECT * FROM `logincodes` WHERE `id` = '".$mysql->real_escape_string($this->id)."' LIMIT 1";
$result = $mysql->query($sql);
if($result->num_rows > 0) {
$info = $result->fetch_assoc();
if(strcmp($info["id"], $this->id) == 0) {
$this->setHash($info["authhash"]);
$this->setUser($info["userid"]);
$this->hasLoggedIn = true;
return true;
} else {
return false;
}
} else {
return false;
}
}
}
$session = new Session();