-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCookie.class.php
More file actions
125 lines (105 loc) · 2.97 KB
/
Cookie.class.php
File metadata and controls
125 lines (105 loc) · 2.97 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/*
Usando
// Preferência de estilo, persiste apenas até o navegador é fechado
Cookie::Set('style', 'black_and_orange', Cookie::Session);
// Lembre-se o endereço de email que os usuários pré-preencher o formulário de login quando eles retornam
Cookie::Set('rememberme', 'email@domain.com', Cookie::trintaDias);
// Cookie de rastreamento que efetivamente dura para sempre
Cookie::Set('tracking', 'sdfoiwuyo8who8wfhow8fhso4', Cookie::Lifetime, '/', '.domain.com');
Site referencia: http://3ft9.com/snippet-cookie-class-for-php/
*/
class Cookie
{
const Session = null;
const umDia = 86400;
const seteDias = 604800;
const trintaDias = 2592000;
const seisMeses = 15811200;
const umAno = 31536000;
const Lifetime = -1; // 2030-01-01 00:00:00
/**
* Returns true if there is a cookie with this name.
*
* @param string $name
* @return bool
*/
static public function Exists($name)
{
return isset($_COOKIE[$name]);
}
/**
* Returns true if there no cookie with this name or it's empty, or 0,
* or a few other things. Check http://php.net/empty for a full list.
*
* @param string $name
* @return bool
*/
static public function IsEmpty($name)
{
return empty($_COOKIE[$name]);
}
/**
* Get the value of the given cookie. If the cookie does not exist the value
* of $default will be returned.
*
* @param string $name
* @param string $default
* @return mixed
*/
static public function Get( $name, $default = '')
{
return ( isset($_COOKIE[$name]) ? $_COOKIE[$name] : $default );
}
/**
* Set a cookie. Silently does nothing if headers have already been sent.
*
* @param string $name
* @param string $value
* @param mixed $expiry
* @param string $path
* @param string $domain
* @return bool
*/
static public function Set( $name , $value , $expiry = self::umAno , $path = '/' , $domain = false )
{
$retval = false;
if (!headers_sent())
{
if ($domain === false)
$domain = $_SERVER['HTTP_HOST'];
if ($expiry === -1)
$expiry = 1893456000; // Lifetime = 2030-01-01 00:00:00
elseif (is_numeric($expiry))
$expiry += time();
else
$expiry = strtotime($expiry);
$retval = @setcookie($name, $value, $expiry, $path, $domain);
if ($retval)
$_COOKIE[$name] = $value;
}
return $retval;
}
/**
* Delete a cookie.
*
* @param string $name
* @param string $path
* @param string $domain
* @param bool $remove_from_global Set to true to remove this cookie from this request.
* @return bool
*/
static public function Delete( $name , $path = '/', $domain = false, $remove_from_global = false )
{
$retval = false;
if (!headers_sent())
{
if ($domain === false)
$domain = $_SERVER['HTTP_HOST'];
$retval = setcookie($name, '', time() - 3600, $path, $domain);
if ($remove_from_global)
unset($_COOKIE[$name]);
}
return $retval;
}
}