Last active
October 30, 2017 11:37
-
-
Save bitsmanent/aae7c5167e52f7219794b8a3794ac7dd to your computer and use it in GitHub Desktop.
Single-routine PHP session handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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