-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_token.php
More file actions
72 lines (63 loc) · 2.38 KB
/
class_token.php
File metadata and controls
72 lines (63 loc) · 2.38 KB
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
<?php
/*****
php class token
http://code.google.com/p/php-class-token/
owner: Sro [sro.co.il]
*****/
class token
{
public $timeout = 1800; // 1800 sec is 30 min for time out
public $tokenfieldname = "token";
public $error = "";
private $errormsg1 = "token not received";
private $errormsg2 = "token failed";
private $errormsg3 = "token time out, make the action again";
function createtoken()
{
// create new token, prefix (to distinguish with another session date) with random value (to prevent guess)
$token = "token-" . mt_rand();
// put in the token, created time
$_SESSION[$token] = time();
return $token;
}
function createtokenfield()
{
return "<input type='hidden' name='" . $this->tokenfieldname . "' value='" . $this->createtoken() . "' />";
}
function createGETtoken()
{
return $this->tokenfieldname . "=" . $this->createtoken();
}
function checktoken()
{
$tokenfield = $this->tokenfieldname
if (!isset($_REQUEST[$tokenfield]))
{
$this->error = $this->errormsg1;
return false;
}
$token = $_REQUEST[$tokenfield];
if (!isset($_SESSION[$token]))
{
$this->error = $this->errormsg2;
return false;
}
// check if token not time outing
if (time() - $_SESSION[$token] < $this->timeout)
{
// after the validation, we clear the specifically session to prevent repeating on the same token
unset($_SESSION[$token]);
return true;
}
else
{
$this->error =$this->errormsg3;
return false;
}
}
}
if(!isset($_SESSION))
session_start();
if(!isset($token))
$token = new token;
?>