-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPHCO.php
140 lines (130 loc) · 4.42 KB
/
PHCO.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* Class PHCO
* Author: Sakibur Rahman @sakibweb
* A utility class for managing HTTP cookies in PHP applications.
* Provides methods for adding, updating, removing, and accessing cookies.
*/
class PHCO {
/**
* Adds a new cookie or updates an existing one with the specified name and value.
*
* @param string $name The name of the cookie.
* @param mixed $value The value to set for the cookie.
* @param int|null $expireMinutes The expiration time for the cookie in minutes. Null for session cookie.
* @return bool True on success, false on failure.
*/
public static function add($name, $value, $expireMinutes = null) {
if ($expireMinutes !== null && is_numeric($expireMinutes)) {
$expireTime = time() + ($expireMinutes * 60);
} elseif ($expireMinutes === 0) {
$expireTime = time() - 3600;
} else {
$expireTime = 0;
}
return setcookie($name, $value, $expireTime, '/');
}
/**
* Updates the value and expiration time of an existing cookie or creates a new one if not exists.
*
* @param string $name The name of the cookie.
* @param mixed $value The new value to set for the cookie.
* @param int|null $expireMinutes The new expiration time for the cookie in minutes. Null for session cookie.
* @return bool True on success, false on failure.
*/
public static function update($name, $value, $expireMinutes = null) {
if (self::exists($name)) {
self::remove($name);
}
return self::add($name, $value, $expireMinutes);
}
/**
* Removes a cookie with the specified name.
*
* @param string $name The name of the cookie to remove.
* @return bool True on success, false on failure.
*/
public static function remove($name) {
if (self::exists($name)) {
unset($_COOKIE[$name]);
return setcookie($name, '', time() - 3600, '/');
}
return false;
}
/**
* Retrieves the value of a cookie with the specified name.
*
* @param string $name The name of the cookie.
* @return mixed|null The value of the cookie if exists, null otherwise.
*/
public static function get($name) {
return $_COOKIE[$name] ?? null;
}
/**
* Checks if a cookie with the specified name exists.
*
* @param string $name The name of the cookie.
* @return bool True if the cookie exists, false otherwise.
*/
public static function exists($name) {
return isset($_COOKIE[$name]);
}
/**
* Checks if a cookie with the specified name has expired.
*
* @param string $name The name of the cookie.
* @return bool True if the cookie has expired, false otherwise or if not exists.
*/
public static function expired($name) {
if (self::exists($name)) {
$cookieValue = $_COOKIE[$name];
$expireTime = (int)$cookieValue;
return $expireTime < time();
}
return true;
}
/**
* Checks if a cookie with the specified name is active (not expired).
*
* @param string $name The name of the cookie.
* @return bool True if the cookie is active (not expired), false otherwise or if not exists.
*/
public static function active($name) {
return !self::expired($name);
}
/**
* Retrieves the remaining time until expiration of a cookie with the specified name.
*
* @param string $name The name of the cookie.
* @return int|null The remaining time until expiration in seconds, or null if not exists.
*/
public static function getExpiredDetails($name) {
if (self::exists($name)) {
$expireTime = $_COOKIE[$name];
return $expireTime - time();
}
return null;
}
/**
* Sets a cookie with the specified name to expired.
*
* @param string $name The name of the cookie to expire.
* @return bool True on success, false on failure or if not exists.
*/
public static function makeExpired($name) {
if (self::exists($name)) {
$_COOKIE[$name] = time() - 3600;
return setcookie($name, '', time() - 3600, '/');
}
return false;
}
/**
* Retrieves all available cookies.
*
* @return array An associative array of cookies.
*/
public static function getAll() {
return $_COOKIE;
}
}
?>