Return to Snippet

Revision: 61008
at November 22, 2012 11:14 by halk


Initial Code
/**
* 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(){
        if (session_id() == false){
            session_start();
            session_regenerate_id();//vs session hijacking
        }
    }
    
    /**
* 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) {
        if (is_array($value)){ //if value is an array update and existing variable
            $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(isset($_SESSION[$key])){
            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(isset($_SESSION[$key])){
            if($key2){
               unset($_SESSION[$key][$key2]);
            }else{
               unset($_SESSION[$key]);
            }
        }
    }
    
    /**
* Session::destroy()
* Destroy The Current Session With All Variables
* @return void
*/
    public static function destroy(){
        if (session_id() == true){
            session_destroy();
        }
    }
    
    /**
* Session::dump()
* Display The Current $_SESSION Array
* (for debugging purposes)
* @return void
*/
    public static function dump(){
        if (session_id() == true){
            echo '<pre>';
            print_r($_SESSION);
            echo '</pre>';
        }
    }
}

//EXAMPLES OF USE
Session::start(); //Start  a Session
$session_array = array(
                "username"=>"brucewayne",
                "logged_in"=>"10/21/12",
                "role"=>"Super-User Admin"
        );
Session::set("user",$session_array);
Session::set("singleton","one");        
Session::set("user",array("role"=>"head honcho","specialty"=>"vigilante crime fighting"));
$singleton = Session::get("singleton");
$username = Session::get("user","username");
Session::del("user","role");
Session::del("singleton");
Session::destroy();

Initial URL


Initial Description
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)

Initial Title
Secure Static Session Class (Handles Arrays)

Initial Tags
class, php

Initial Language
PHP