Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, one option would be to use a session class to start/stop/store data in the session. So, you could do something like:</p> <pre><code>class Session implements ArrayAccess { protected $closed = false; protected $data = array(); protected $name = 'mySessionName'; protected $started = false; protected function __construct() { if (isset($_COOKIE[$this-&gt;name])) $this-&gt;start(); $this-&gt;data = $_SESSION; } public static function initialize() { if (is_object($_SESSION)) return $_SESSION; $_SESSION = new Session(); register_shutdown_function(array($_SESSION, 'close')); return $_SESSION; } public function close() { if ($this-&gt;closed) return false; if (!$this-&gt;started) { $_SESSION = array(); } else { $_SESSION = $this-&gt;data; } session_write_close(); $this-&gt;started = false; $this-&gt;closed = true; } public function offsetExists($offset) { return isset($this-&gt;data[$offset]); } public function offsetGet($offset) { if (!isset($this-&gt;data[$offset])) { throw new OutOfBoundsException('Key does not exist'); } return $this-&gt;data[$offset]; } public function offsetSet($offset, $value) { $this-&gt;set($offset, $value); } public function offsetUnset($offset) { if (isset($this-&gt;data[$offset])) unset($this-&gt;data[$offset]); } public function set($key, $value) { if (!$this-&gt;started) $this-&gt;start(); $this-&gt;data[$key] = $value; } public function start() { session_name($this-&gt;name); session_start(); $this-&gt;started = true; } } </code></pre> <p>To use, at the start of your script call <code>Session::initialize()</code>. It will replace $_SESSION with the object, and setup the lazy loading. Afterward, you can just do</p> <pre><code>$_SESSION['user_id'] = 1; </code></pre> <p>If the session isn't started, it will be, and the user_id key would be set to 1. If at any point you wanted to close (commit) the session, just call <code>$_SESSION-&gt;close()</code>. </p> <p>You'll probably want to add some more session management functions (such as destroy, regenerate_id, the ability to change the name of the session, etc), but this should implement the basic functionality you're after...</p> <p>It's not a save_handler, it's just a class to manage your sessions. If you really wanted to, you could implement ArrayAccess in the class, and on construct replace $_SESSION with that class (The benefit of doing that, is that way legacy code can still use session as they used to without calling <code>$session-&gt;setData()</code>). The only downside is that I'm not sure if the serialization routine that PHP uses would work properly (You'd need to put back the array into $_SESSION at some point... Probably with a <code>register_shutdown_function()</code>...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload