Location: PHPKode > scripts > Locked Session Handler > locked-session-handler/session.class.php
<?php
/*******************************************************************************
 *
 *  filename    : session.class.php
 *  description : Core Session Management Class
 *  authors     : Lewis Franklin <hide@address.com>
 *
 *  GroupOrg is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  $Id: session.class.php,v 1.5 2004/09/04 16:37:38 brolewis Exp $
 ******************************************************************************/
/**
 * Session Management
 * @author brolewis
 * @copyright LGPL
 * @access public
 **/
class Session {
	/**
	 * Initialize the session to make sure the lockList is present and
	 * unwritable
	 **/
	function __construct() {
		if (!$this->isSess("lockList")) {
			$lockList = array();
			$lockList[] = "lockList";
			$_SESSION["lockList"] = $lockList;
		}
	}

    /**
     * Sets a value in the session, checking first to make sure the 
	 * variable isn't locked and the name is valid.
     * @param string $name name of variable stored in session
     * @param mixed $value value of variable stored in session
     **/
    function setSess($name, $value)
    {
		if (preg_match("!^[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*$!i", $name)){
			$lockList = $this->getSess("lockList");
			if (in_array($name, $lockList)) {
			    trigger_error("'<i>$name</i>' is locked and cannot be set", E_USER_NOTICE);
			} else {
				$_SESSION[$name] = $value;
			}
        } else {
			trigger_error("Invalid variable name", E_USER_NOTICE);
		}
    }

    /**
     * Gets a value from session
     * @param string $name nmae of variable checked in session
     * @return mixed Value in session or null if not found
     **/
    function getSess($name)
    {
		if (isset($_SESSION[$name])) {
		    return $_SESSION[$name];
		} else {
	        return null;	
		}
    }

    /**
     * Checks to see if a variable is in session
     * @param string $name name of variable checked in session
     * @return boolean true if found, false if not
     **/
    function isSess($name)
    {
        return ($this->getSess($name) !== null);
    }

    /**
     * Delete a key/value pair in session, checking first to make sure 
	 * the key is not locked.
     * @param string $name name of variable to be removed from session
     **/
    function delSess($name)
    {
		$lockList = $this->getSess("lockList");
		if (in_array($name, $lockList)) {
			trigger_error("'<i>$name</i>' is locked and cannot be deleted", E_USER_NOTICE);
		} else {
	        unset($_SESSION[$name]);		
		}
    }

	/**
	 * Locks a key/value pair in the session so that it cannot be overwritten
	 * @param string $name name of the variable to be locked in session
	 **/
	function lockSess($name)
	{
	    $lockList = $this->getSess("lockList");
		if ($this->isSess($name)) {
			if (in_array($name, $lockList)) {
				trigger_error("'<i>$name</i>' has already been locked", E_USER_NOTICE);
			} else {
				$lockList[] = $name;
				$_SESSION["lockList"] = $lockList;
			}
		} else {
			trigger_error("There is no session variable called '<i>$name</i>'", E_USER_NOTICE);
		}
	}

	/**
	 * Unlocks a key/value parin in the session so that it can be updated
	 * @param string $name name of variable to be unlocked in session.
	 * @return 
	 **/
	function unlockSess($name)
	{
		$lockList = $this->getSess("lockList");
		if ($key = array_search($name, $lockList)) {
			unset($lockList[$key]);
			$_SESSION["lockList"] = $lockList;
		} else {
		    trigger_error("'<i>$name</i>' is not locked.", E_USER_NOTICE);
		}
	}

	/**
     * Kills the entire session. This is VERY dangerous to use. Use with caution!
     **/
    function killSess()
    {
        session_destroy();
        $_SESSION = array();
    }
}
?>
Return current item: Locked Session Handler