All pastes #565539 Raw Edit

Miscellany

public php v1 · immutable
·published 2007-06-14 01:04 UTC
rendered paste body
<?php############################################################### FILENAME  : class_session.php# COPYRIGHT : (c) 2007, Tyler N. King <gotbase.org># GPL v2.0 or later. See docs/COPYING.##############################################################/** * Session Emulation Class. * This class manages sessions without using the built-in php session handler. */class session{	/**	 * Contains a associative array of the session row in the database.	 *	 * @var array	 */	var $data;	/**	 * Contains the session_id of the active session.	 *	 * @var string	 */	var $sid;	/**	 * Contains the method in which sessions are being handled, $_GET or $_COOKIE.	 *	 * @var string	 */	var $method;	/**	 * Init the session object, get the user details, create or update session row.	 *	 * @return object	 */	function session ( )	{		global	$config;		global	$db;		global	$_SERVER;		global	$_GET;		global	$_COOKIE;		// Set the class variables.		$this->method	= ( ! empty ( $_COOKIE[ '_sid' ] ) ) ? 'cookie' : 'get';		$this->sid	= ( $this->method == 'cookie' ) ? $_COOKIE[ '_sid' ] : $_GET[ 'sid' ];		// Delete expired sessions.		$expire_time	= ( time() - ( 60 * 60 ) );		$sql		= 'DELETE FROM ' . SESSIONS_TABLE . ' WHERE session_time < ' . $expire_time;		if ( ! $db->sql_query ( $sql ) )		{			trigger_error ( 'session::error, Unable to delete expired sessions.' );		}		// Check if the data has to be created or updated.		if ( $row = $this->select ( $this->sid ) )		{			// Session exists, update the current session data.			$this->update ( );		}		else		{			// Session does not exist, create the session data.			$this->create ( );		}se.		return;	}	/**	 * Create a new session and return the session ID.	 *	 * @return string	 */	function create ( )	{		global	$db;		global	$config;				// Create the session ID.		$this->sid	= md5 ( time ( ) . rand ( ) . range ( 'a', 'z' ) );				// Insert session data into the database.		$sql	= 'INSERT INTO ' . SESSIONS_TABLE . ' (session_id, session_time, user_id) VALUES ("' . $this->sid . '", ' . time ( ) . ', 0)';		$db->sql_query ( $sql );				// Set session data.		$this->select ( $this->sid );				// Create a cookie.		setcookie ( '_sid', $this->sid );				return	$this->sid;	}	/**	 * This function will update a session with new data.	 *	 * @return string	 */	function update ( )	{		global	$db;				// Update the session.		$sql	= 'UPDATE ' . SESSIONS_TABLE . ' SET session_time = ' . time ( ) . ' WHERE session_id = ' . $db->sql_escape ( $this->sid );		$db->sql_query ( $sql );		// Try setting the cookie, again.		if ( $this->method == 'get' )		{			setcookie ( '_sid', $this->sid );		}				return	$this->sid;	}		/**	 * This function will select the session data from the database.	 *	 * @param string $session_id	 * @return array	 */	function select ( $session_id = false )	{		global	$db;		// Set the SID if it is not  set already.		$session_id	= ( $session_id !== false ) ? $session_id : $this->sid;		// Select session data from the database.		$sql	= 'SELECT * FROM ' . SESSIONS_TABLE . ' WHERE session_id = ' . $db->sql_escape ( $session_id );		$result	= $db->sql_query ( $sql );		// Attempt to select the row.		if ( $row = $db->sql_fetchrow ( $result ) )		{			// Set the session data and return the row.			$this->data	= $row;			return	$row;		}		return	false;	}	/**	 * This function will instantly expire a session.	 *	 * @param string $session_id	 */	function purge ( $session_id = false )	{		global	$db;		global	$config;		// Set the SID if it is not  set already		$session_id	= ( $session_id !== false ) ? $session_id : $this->sid;				// Delete the session from the database.		$sql	= 'DELETE FROM ' . SESSIONS_TABLE . ' WHERE session_id = ' . $db->sql_escape ( $session_id );		$db->sql_query ( $sql );				// Delete cookie, if the user owns it.		if ( $session_id == $this->sid )		{			setcookie ( '_sid', '' );		}		return;	}}?>