<?php /** * Description: * Functions for session accessing * * History: * Created by Moky @ 2008-07-26 */ /** Samples: include_once('include/lib/session.php'); setSession('moky', 9527); echo getSession('moky'); //delSession('moky'); // delete one session //clearSession(); // clear all sessions */ // disallows any client/proxy caching session_cache_limiter('nocache'); // session id $sessid = isset($_GET['sessid']) ? $_GET['sessid'] : NULL; if (!$sessid) { session_start(); $sessid = session_id(); } else { session_id($sessid); session_start(); } function getSession($name) { $skey = 'skey_' . $name; // private key name return loadSession($skey); } function setSession($name, $value) { $skey = 'skey_' . $name; // private key name storeSession($skey, $value); saveSessionKey($skey); } function delSession($name) { $skey = 'skey_' . $name; // private key name return eraseSession($skey); } function clearSession() { return eraseAllSessions(); } //----------------------------------------------------- function storeSession($skey, $value) { if (!isset(${$skey})) { session_register($skey); } ${$skey} = $value; $_SESSION[$skey] = $value; } function loadSession($skey) { if (!isset(${$skey})) { session_register($skey); } $value = isset(${$skey}) ? ${$skey} : NULL; if ($value == NULL) { $value = $_SESSION[$skey]; } return $value; } function eraseSession($skey) { session_unregister($skey); $_SESSION[$skey] = NULL; ${$skey} = NULL; unset($skey); } //----------------------------------------------------- function saveSessionKey($key) { $keys = loadSession('stored_session_keys'); if (!$keys) { $keys = $key; } else if (strpos("||$keys|", "|$key|") > 0) { return ; } else { $keys .= "|$key"; } storeSession('stored_session_keys', $keys); } function eraseAllSessions() { $keys = loadSession('stored_session_keys'); if (!$keys) return ; $keys = explode('|', $keys); foreach ($keys as $key) { eraseSession($key); } eraseSession('stored_session_keys'); } ?>