chrisdempsey
8/1/2017 - 1:36 PM

SessionManager: A script that can be used for easy control over your session variables

SessionManager: A script that can be used for easy control over your session variables

<?php
/**
 * SessionManager
 * A script that can be used for easy control over your session variables.
 * Put this script in your core/components/ folder, inside a sessionman/ subdirectory.
 * 
 * Load this one in your snippets/plugins like;
 * 
 * $sessman = $modx->getService('session','SessionManager', $modx->getOption('sessman.core_path',null,$modx->getOption('core_path').'components/sessionman/'), $scriptProperties);
 * if(!($sessman instanceof SessionManager)) { $modx->log(modX::LOG_LEVEL_ERROR, 'Session manager could not be loaded..'); return ''; }
 *
 * Within your snippets you can use it like:
 * 
 *	$modx->session->set('varName', 'value');
 *	$modx->session->has('varName');
 *	$modx->session->get('varName');
 *	$modx->session->remove('varName');
 *	$modx->session->getAll();
 *	$modx->session->removeAll();
 * 
 * You also can do multiple actions at once like:
 * 
 * 	$modx->session
 * 		->set('varName1', 'value1')
 * 		->set('varName2', 'value2');
 * 
 * 	$modx->session
 * 		->remove('varName1')
 * 		->remove('varName2');
 *
 * Or mix them up;
 * 
 * 	$modx->session
 * 		->set('varName1', 'value1')
 * 		->remove('varName2');
 * 
 * Within the whole session manager there is a prefix used to all the variables
 * these prefix can be changed from default "modx.pkg." to anything else with
 * the method $modx->session->setPrefix('your.new.prefix.');
 * 
 * @author Bert Oost at OostDesign.nl <bert@oostdesign.nl>
 * @author Jeroen Kenters <jeroen@kenters.com> (Thanks for the phpdoc)
 */
class SessionManager
{
	private $sessId;
	private $prefix = 'modx.pkg.';
	
	public function __construct() {
		
		$sessid = session_id();
		if(empty($sessid)) {
			session_start();
			$sessid = session_id();
		}
		
		$this->sessId = $sessid;
	}
	
	/**
	 * Returns the current session ID
	 * @return string
	 */
	public function getId() {
		return $this->sessId;
	}
	
	/**
	 * Sets the prefix for all session variables
	 * @param string $prefix
	 * @return object
	 */
	public function setPrefix($prefix='modx.pkg.') {
		if(!empty($prefix)) {
			$this->prefix = $prefix;
		}
		return $this;
	}
	
	/**
	 * Sets an variable name into the session
	 * @param string $var The name of the variable to set
	 * @param mixed $value The value of the variable
	 * @return object
	 */
	public function set($var, $value=null) {
		
		if(!empty($var)) {
			
			if(isset($_SESSION[$this->prefix.$var])) {
				unset($_SESSION[$this->prefix.$var]);
			}
			
			$_SESSION[$this->prefix.$var] = $value;
		}
		
		return $this;
	}
	
	/**
	 * Use to check if the session contains a given variable
	 * @param string $var The name of the variable
	 * @return boolean
	 */
	public function has($var) {
		
		if(!empty($var) && isset($_SESSION[$this->prefix.$var])) {
			
			return true;
		}
		
		return false;
	}
	
	/**
	 * Returns the value of a setted session variable
	 * @param string $var The name of the variable to get
	 * @param mixed $default The default value when empty
	 * @return mixed Contents of the session variable or boolean false
	 */
	public function get($var, $default='') {
		
		if(!empty($var) && $this->has($var)) {
			
			if(empty($_SESSION[$this->prefix.$var]) && !empty($default)) {
				
				return $default;
			}
			
			return $_SESSION[$this->prefix.$var];
		}
		
		return false;
	}
	
	/**
	 * Removes a session variable from the session
	 * @param string $var The name of the variable to remove
     	 * @return oject
	 */
	public function remove($var) {
		
		if(!empty($var) && $this->has($var)) {
			
			unset($_SESSION[$this->prefix.$var]);
		}
		
		return $this;
	}
	
	/**
	 * Gets all session variables which are currently set
	 * @return array $vars List of vars set
	 */
	public function getAll() {
		$vars = array();
		foreach($_SESSION as $key => $value) {
			if(substr($key, 0, strlen($this->prefix)) != $this->prefix) { continue; }
			$key = substr($key, strlen($this->prefix));
			$vars[$key] = $value;
		}
		return $vars;
	}
	
	/**
	 * Removes all session variables which are currently set
	 * @return object
	 */
	public function removeAll() {
		foreach($_SESSION as $key => $value) {
			if(substr($key, 0, strlen($this->prefix)) != $this->prefix) { continue; }
			unset($_SESSION[$key]);
		}
		return $this;
	}
}