Skip to content

Instantly share code, notes, and snippets.

@zepich
Last active August 29, 2015 14:05
Show Gist options
  • Save zepich/6cdc4319319efa1daf8a to your computer and use it in GitHub Desktop.
Save zepich/6cdc4319319efa1daf8a to your computer and use it in GitHub Desktop.
Custom Session Handler
<?php
class SessionSaveHandler
{
public function __construct()
{
session_set_save_handler(
array($this, 'open'),
array($this, 'close'),
array($this, 'read'),
array($this, 'write'),
array($this, 'destroy'),
array($this, 'gc')
);
register_shutdown_function('session_write_close');
}
public function open($savePath, $sessionName)
{
return true;
}
public function close()
{
return true;
}
public function read($id)
{
global $memcachedConnection;
return 'data';
}
public function write($id, $data)
{
global $memcachedConnection;
// save data
// return true; // This is the important point. If you uncomment this line, everything works fine
}
public function destroy($id)
{
global $memcachedConnection;
// destroy the session
return true;
}
public function gc($maxlifetime)
{
return true;
}
}
// Initialize the session save handler
$sessionHandler = new SessionSaveHandler();
// Start the session
session_start();
// Set the user-id
$_SESSION['user-id'] = 1;
// Display something
echo 'user-id stored';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment