Skip to content

Commit 17ab291

Browse files
committed
creation of the exist function
1 parent acebb52 commit 17ab291

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

cookie.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ function __construct($name, $value, $time)
2020
# code...
2121
}
2222

23+
// CHECK IF THE COOKIE EXISTS
24+
public function exist()
25+
{
26+
return (isset($_COOKIE[$this->cookie_name]))? 1:0;
27+
# code...
28+
}
29+
2330
#LAUNCH
2431
public function start()
2532
{
@@ -33,14 +40,14 @@ public function start()
3340
public function update($time)
3441
{
3542
// CHECKING IF THE COOKIE ALREADY EXIST
36-
if (isset($_COOKIE[$this->cookie_name])) {
43+
if ($this->exist()== 1) {
3744
$this->cookie_value = $_COOKIE[$this->cookie_name];
3845
$this->time = $time;
3946
$this->start();
4047
return 1;
4148
# code...
4249
} else {
43-
return "message d'erreur";
50+
return 0;
4451
}
4552

4653
# code...
@@ -49,18 +56,17 @@ public function update($time)
4956
#READ COOKIE VALUE
5057
public function getValue()
5158
{
52-
return $this->cookie_value;
59+
($this->exist()== 1)? $this->cookie_value:trigger_error("session cookie does not exist", E_USER_ERROR);
5360
# code...
5461
}
5562

5663
#DESTRUCTION
5764
public function stop()
5865
{
59-
// Suppression du cookie
66+
// deletion of the cookie
6067
setcookie($this->cookie_name);
61-
// Suppression de la valeur du tableau $_COOKIE
68+
// Removed super global value $_COOKIE
6269
unset($_COOKIE[$this->cookie_name]);
63-
// redirection
6470
return 1;
6571

6672
# code...

index.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@
55
require_once './cookie.php';
66

77
// MAIN PROGRAMM
8+
const COOKIE_NAME = "the name of the cookie dude";
9+
const DEFAULT_COOKIE_TIME = 60*60*24*365; # one year duration
10+
$cookie_value = "give a value to your cookie";
811

912
// INSTANTIATION
10-
$cookie = new CookieSession('user', 'devcarle@gmail.com', time() + 15 * 3);
13+
$cookie = new CookieSession(COOKIE_NAME, $cookie_value, time() + DEFAULT_COOKIE_TIME);
1114

1215
// LAUNCH
1316
$answer = $cookie->start();
14-
// header("Location: ./");
17+
// you must redirect to another page for the cookie to exist
18+
#header("Location: ./");
1519

1620
// UPDATE
1721
$answer = $cookie->update(time() + 60 * 60);
1822

1923
// GET VALUE
2024
$cookie_value = $cookie->getValue();
21-
#echo "hey";
2225
// DESTRUCTION
26+
2327
$answer = $cookie->stop();
28+
// redirection to the login page or somethinglike
29+
#header("Location: ./");

0 commit comments

Comments
 (0)