Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
PhpSession.php
Go to the documentation of this file.
1<?php
2
4
15
16 public function set(string $key, $value) {
17 return $_SESSION [$key] = $value;
18 }
19
20 public function get(string $key, $default = null) {
21 return $_SESSION [$key] ?? $default;
22 }
23
24 public function start(string $name = null) {
25 if (! $this->isStarted ()) {
26 if (isset ( $name ) && $name !== '') {
27 $this->name = $name;
28 }
29 if (isset ( $this->name )) {
30 \session_name ( $this->name );
31 }
32 \session_start ();
33 $this->verifyCsrf->start ();
34 }
35 }
36
37 public function terminate(): void {
38 if (! $this->isStarted ()) {
39 return;
40 }
41 $this->verifyCsrf->clear ();
42 $_SESSION = [];
43
44 if (\ini_get ( 'session.use_cookies' )) {
45 $params = \session_get_cookie_params ();
46 \setcookie ( \session_name (), '', \time () - 42000, $params ['path'], $params ['domain'], $params ['secure'], $params ['httponly'] );
47 }
48 \session_destroy ();
49 }
50
51 public function isStarted(): bool {
52 return \session_status () == PHP_SESSION_ACTIVE;
53 }
54
55 public function exists(string $key): bool {
56 return isset ( $_SESSION [$key] );
57 }
58
59 public function getAll(): array {
60 return $_SESSION;
61 }
62
63 public function delete(string $key) {
64 unset ( $_SESSION [$key] );
65 }
66
67 public function regenerateId(bool $deleteOldSession=false):bool {
68 return \session_regenerate_id($deleteOldSession);
69 }
70
71 public function visitorCount(): int {
72 $sessionPath = \ini_get ( 'session.save_path' );
73 $sessionLifetime = \ini_get ( 'session.gc_maxlifetime' );
74 $files = glob ( $sessionPath . DS . 'sess_*' );
75 $now = time ();
76 $count = 0;
77 foreach ( $files as $file ) {
78 if (is_file ( $file )) {
79 if ($now - filemtime ( $file ) <= $sessionLifetime) {
80 $count ++;
81 }
82 }
83 }
84 return $count;
85 }
86}
87
Ubiquity\utils\http\session$AbstractSession This class is part of Ubiquity.
regenerateId(bool $deleteOldSession=false)
Re-generates the session id.