Posts phpsession
Post
Cancel

phpsession

source http://github.com/joefallon/phpsession

Allows a maximum age to be used for session invalidation. Allows a maximum inactivity time to be used for session invalidation.

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//Session.php
<?php namespace="" joefallon\phpsession;="" use="" invalidargumentexception;="" **="" *="" @author="" joseph="" fallon=""?><joseph.t.fallon@gmail.com>
 * @copyright Copyright 2014 Joseph Fallon (All rights reserved)
 * @license   MIT
 */
class Session
{
    /** @var int */
    private $_maxAgeInSecs;
    /** @var int */
    private $_lastActivityTimeoutInSecs;
    /**
     * The value of $maxAgeInSecs must be greater than or equal to
     * $lastActivityTimeoutInSecs. To disable maximum session
     * age checks, set the $maxAgeInSecs value to zero. To disable the
     * checks on last activity timeout, set the value of
     * $lastActivityTimeoutInSecs to zero.
     *
     * @param int $maxAgeInSecs
     * @param int $lastActivityTimeoutInSecs
     *
     * @throws InvalidArgumentException
     */
    public function __construct($maxAgeInSecs = 1800, $lastActivityTimeoutInSecs = 1800)
    {
        $maxAgeInSecs              = intval($maxAgeInSecs);
        $lastActivityTimeoutInSecs = intval($lastActivityTimeoutInSecs);
        if($maxAgeInSecs < 0)="" {="" $msg='The session max age is less than zero.' ;="" throw="" new="" invalidargumentexception($msg);="" }="" if($lastactivitytimeoutinsecs="">< 0)="" {="" $msg='The session last activity timeout is less than zero.' ;="" throw="" new="" invalidargumentexception($msg);="" }="" if($maxageinsecs="">< $lastactivitytimeoutinsecs)="" {="" $msg='The session max age must be longer than last activity timeout.' ;="" throw="" new="" invalidargumentexception($msg);="" }="" $this-="">_maxAgeInSecs              = $maxAgeInSecs;
        $this->_lastActivityTimeoutInSecs = $lastActivityTimeoutInSecs;
    }
    /**
     * read - This function retrieves the value associated with the key.
     *
     * @param string $key
     *
     * @return null
     */
    public function read($key)
    {
        $key = strval($key);
        $val = null;
        if(strlen($key) == 0)
        {
            return $val;
        }
        $this->openSession();
        if(isset($_SESSION[$key]) == true)
        {
            $val = $_SESSION[$key];
        }
        $this->closeSession();
        return $val;
    }
    /**
     * write - This function writes a session value to the session.
     *
     * @param string $key Key to store the session value in.
     * @param string $val Value to be stored.
     *
     * @throws InvalidArgumentException
     */
    public function write($key, $val)
    {
        $key = strval($key);
        if(strlen($key) == 0)
        {
            $msg = 'The key cannot be empty.';
            throw new InvalidArgumentException($msg);
        }
        $this->openSession();
        $_SESSION[$key] = $val;
        $this->closeSession();
    }
    /**
     * unsetSessionValue - This function unsets a session variable.
     *
     * @param string $key
     */
    public function unsetSessionValue($key)
    {
        $key = strval($key);
        $this->openSession();
        unset($_SESSION[$key]);
        $this->closeSession();
    }
    /**
     * maxAgeExpired
     *
     * @return boolean This function returns true if the time limit between now
     *                 and the time that the session was started has been
     *                 exceeded.
     */
    public function maxAgeTimeoutExpired()
    {
        if($this->_maxAgeInSecs == 0)
        {
            return false;
        }
        $this->openSession();
        if(!isset($_SESSION['session_created_time']))
        {
            $_SESSION['session_created_time'] = time();
            $this->closeSession();
            return false;
        }
        else
        {
            $now     = time();
            $created = intval($_SESSION['session_created_time']);
            $max     = $this->_maxAgeInSecs;
            $diff    = $now - $created;
            if($diff >= $max)
            {
                $this->closeSession();
                return true;
            }
            $this->closeSession();
            return false;
        }
    }
    /**
     * lastActivityTimeoutExpired
     *
     * @return boolean This function returns true if the time limit between now
     *                 and the last activity has been exceeded.
     */
    public function lastActivityTimeoutExpired()
    {
        if($this->_lastActivityTimeoutInSecs == 0)
        {
            return false;
        }
        $this->openSession();
        if(!isset($_SESSION['session_last_activity_time']))
        {
            $_SESSION['session_last_activity_time'] = time();
            $this->closeSession();
            return false;
        }
        else
        {
            $now          = time();
            $lastActivity = intval($_SESSION['session_last_activity_time']);
            $max          = $this->_lastActivityTimeoutInSecs;
            $diff         = $now - $lastActivity;
            if($diff >= $max)
            {
                $this->closeSession();
                return true;
            }
            $this->closeSession();
            return false;
        }
    }
    /**
     * destroy - This function is used to completely eliminate a session.
     */
    public function destroy()
    {
        session_start();
        if(ini_get("session.use_cookies"))
        {
            $params = session_get_cookie_params();
            setcookie(session_name(),
                      '',
                      time() - 42000,
                      $params["path"],
                      $params["domain"],
                      $params["secure"],
                      $params["httponly"]
            );
        }
        session_unset();
        session_destroy();
        session_regenerate_id(true);
        session_write_close();
    }
    /**
     * openSession
     */
    private function openSession()
    {
        session_start();
        session_regenerate_id();
        $_SESSION['session_last_activity_time'] = time();
    }
    /**
     * closeSession
     */
    private function closeSession()
    {
        session_write_close();
    }
}

```js //example $foo = ‘bar’; $session = new Session(); $session->write($foo, ‘value’); $session->read($foo); // value

The Session class contains the following methods:

__construct($maxAgeInSecs = 1800, $lastActivityTimeoutInSecs = 1800) read($key) write($key, $val) unsetSessionValue($key) maxAgeTimeoutExpired() lastActivityTimeoutExpired() destroy() ```</joseph.t.fallon@gmail.com>

origin - http://www.pipiscrew.com/?p=3116 php-phpsession

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags