/ Published in: PHP
This is a static session wrapper that handles single variables and arrays. It allows setting, resetting, and deleting session variables (without destroying the session). It regenerates the session id every time a session is started to prevent session hijacking. See Examples of use below the code or (https://gist.github.com/4128373)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * Session * -Static Session Wrapper Class * -Convienience Methods (do Error Checking), * -Measures To Prevent Session Hijacking, * -Handles Single Variables or Associative * Arrays through autodetecting the type * passed in. * @package sandboxphp * @author hskitts * @copyright 2012 * @version $Id$ * @access public */ class Session{ /** * Session::start() * Start A Secure Session * Helps Prevent Session Hijacking * @return void */ public static function start(){ } } /** * Session::set() * Sets Session Variables * Handles Arrays and Single Variables by detecting $key type * in the case of $key being an array: * $value will be an array of key value * pairs in the $key array * @param mixed $key * @param mixed $value if key is an array $value must be array * @return void */ public static function set($key, $value) { $ARRAY = $_SESSION[$key]; //save the current session array foreach($value as $k => $v){ //for each key=value pair in the value passed in $ARRAY[$k] = $v; //push } $_SESSION[$key] = $ARRAY; return; } $_SESSION[$key] = $value; } /** * Session::get() * Get The Value Of A Session Variable * (Handles Arrays As well) * @param mixed $key session variable to get * @param bool $key2 only to access an array * @return */ public static function get($key,$key2=false){ if($key2){ return $_SESSION[$key][$key2]; }else{ return $_SESSION[$key]; } }else{ return false; } } /** * Session::del() * Delete A Session Variable * (This Does Not Destroy The * Whole Session) * @param mixed $key * @return void */ public static function del($key,$key2=false){ if($key2){ }else{ } } } /** * Session::destroy() * Destroy The Current Session With All Variables * @return void */ public static function destroy(){ } } /** * Session::dump() * Display The Current $_SESSION Array * (for debugging purposes) * @return void */ public static function dump(){ echo '<pre>'; echo '</pre>'; } } } //EXAMPLES OF USE Session::start(); //Start a Session "username"=>"brucewayne", "logged_in"=>"10/21/12", "role"=>"Super-User Admin" ); Session::set("user",$session_array); Session::set("singleton","one"); $singleton = Session::get("singleton"); $username = Session::get("user","username"); Session::del("user","role"); Session::del("singleton"); Session::destroy();