Skip to content

Instantly share code, notes, and snippets.

@bitsmanent
Last active October 30, 2017 11:37
Show Gist options
  • Save bitsmanent/aae7c5167e52f7219794b8a3794ac7dd to your computer and use it in GitHub Desktop.
Save bitsmanent/aae7c5167e52f7219794b8a3794ac7dd to your computer and use it in GitHub Desktop.
Single-routine PHP session handling
define("SESSION_FILE", "/tmp/session.txt");
$session = [];
function sess($k, $v = NULL) {
global $session;
if($session == NULL) {
if(!file_exists(SESSION_FILE))
touch(SESSION_FILE);
$session = file_get_contents(SESSION_FILE);
$session = $session ? unserialize($session) : [];
register_shutdown_function(function() {
global $session;
file_put_contents(SESSION_FILE, serialize($session));
});
}
$t = @$session[$k];
if($v != NULL)
$session[$k] = $v;
return $t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment