Location: PHPKode > projects > toKernel > tokernel.1.0.6/tokernel.framework/lib/session.lib.php
<?php
/**
 * toKernel - Universal PHP Framework.
 * Class library for working with session.
 * 
 * This file is part of toKernel.
 *
 * toKernel is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * toKernel 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 General Public License
 * along with toKernel. If not, see <http://www.gnu.org/licenses/>.
 *
 * @category   framework
 * @package    toKernel
 * @subpackage library
 * @author     toKernel development team <hide@address.com>
 * @copyright  Copyright (c) 2011 toKernel
 * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @version    1.0.0
 * @link       http://www.tokernel.com
 * @since      File available since Release 1.0.0
 * @todo       Make this library more secure.
 */

/* Restrict direct access to this file */
defined('TK_EXEC') or die('Restricted area.');

/**
 * session_lib class 
 * 
 * NOTE: For future, it is possible to modify 
 * this class for working with cookies also.
 *  
 * @author David Ayvazyan <hide@address.com>
 */
class session_lib {

/**
 * Library object for working with 
 * libraries in this class
 * 
 * @var object
 * @access protected
 */ 
 protected $lib;
 
/**
 * Main Application object for 
 * accessing app functions from this class
 * 
 * @var object
 * @access protected
 */ 
 protected $app;
 
/**
 * Session prefix
 * 
 * @access protected
 * @var string $sp
 */
 protected $sp;

/**
 * Class constructor
 * 
 * @access public
 * @return void
 */ 
 public function __construct() {
	
 	/* 
 	 * Only in this part of framework, 
 	 * session can be start 
 	 */
 	session_start();

 	/* 
 	 * Session save path in application's custom directory 
 	 */
	session_save_path(TK_CUSTOM_PATH . "session");
    
	$this->app = app::instance();
    $this->lib = lib::instance();
    
    /* Set session prefix */
    $app_session_prefix = $this->app->config('session_prefix', 'SESSION');
    
    if($app_session_prefix != '') {
       $this->sp = $app_session_prefix;
    } else {
       $this->sp = 'tokernel_';
    }
    
} // End of func __construct

/**
 * Set session value.
 * 
 * @access public
 * @param string $item
 * @param mixed $value
 * @param string $section
 * @return void
 */
 public function set($item, $value = '', $section = NULL) {
    
	if(!is_null($section)) {
	   $_SESSION[$this->sp . $section][$this->sp . $item] = $value;
	} else {
	   $_SESSION[$this->sp . $item] = $value;
	}
	
 } // end func set

/**
 * Get session value by item, subitem.
 * 
 * @access public
 * @param string $item
 * @param string $section
 * @return mixed
 */
 public function get($item, $section = NULL) {

 	/* Return value by section */
	if(!is_null($section) and isset($_SESSION[$this->sp . 
											$section][$this->sp . $item])) {
												
		return $_SESSION[$this->sp . $section][$this->sp . $item];
	}
		
	if(isset($_SESSION[$this->sp . $item])) {
		return $_SESSION[$this->sp . $item];
	}
	
	return false;
	
 } // end func get

/**
 * Get section as array
 * 
 * @access public
 * @param string $section
 * @return mixed array | mixed
 */
 public function get_section($section) {
	if(isset($_SESSION[$this->sp . $section])) {
		return $_SESSION[$this->sp . $section];
	}
 } // end func get_section
 
/**
 * Unset section
 * 
 * @access public 
 * @param string $section
 * @return bool
 */ 
 public function remove_section($section) {
 	if(isset($_SESSION[$this->sp . $section])) {
		unset($_SESSION[$this->sp . $section]);
		return true;
	} else {
		return false;
	}
 } // end func remove 
 
/**
 * Remove session item
 * 
 * @access public
 * @param string $item
 * @param string $section
 * @return bool
 */
 public function remove($item = NULL, $section = NULL) {

 	/* return section */
 	if(is_null($item) and !is_null($section)) {
 		unset($_SESSION[$this->sp . $section]);
 		return true;
 	}
 	
	if(!is_null($item) and !is_null($section)) {
		unset($_SESSION[$this->sp . $section][$this->sp . $item]);
		return true;
	}

	if(!is_null($item)) {
		unset($_SESSION[$this->sp . $item]);
		return true;
	}
	
	return false;

 } // end func remove

/**
 * Destroy session
 * 
 * @access public
 * @return void
 */
 public function destroy() {
 	session_destroy();
 }

/* End of class session */
}

/* End of file */
?>
Return current item: toKernel