Location: PHPKode > projects > Icecube > icecube/session.php
<?php

//
// Copyright (c) 2006 Brian Rosner
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//

class icecube_session
{
	private $_table;
	private $_primary;
	private $_timeout;
	
	public function __construct($timeout = 10)
	{
		global $db;
		
		$this->_table = $db->get_table_name("session");
		$this->_primary = $db->get_primary_key("session");
		$this->_timeout = 60 * $timeout;
		
		//
		// setup custom session hooks
		//
		session_set_save_handler(
			array($this, "open"),
			array($this, "close"),
			array($this, "read"),
			array($this, "write"),
			array($this, "destroy"),
			array($this, "gc")
		);
		
		//
		// start the session
		//
		session_start();
		
		//
		// fix ie6 bug
		//
		header("Cache-Control: private");
	}
	
	public function open($save_path, $session_name)
	{
		return true;
	}
	
	public function close()
	{
		return true;
	}
	
	public function read($id)
	{
		global $db;
		
		//
		// escape user input
		//
		$id = $db->escape($id);
		
		//
		// fetch session data from database
		//
		$query = sprintf("SELECT `data` FROM {$this->_table} WHERE `{$this->_primary}` = '{$id}' AND `expires` > %d", time());
		return $db->get_var($query);
	}
	
	public function write($id, $data)
	{
		global $db;
		
		//
		// generate a new expiration time for session
		//
		$expires = time() + $this->_timeout;
		
		//
		// escape user input
		//
		$id = $db->escape($id);
		$data = $db->escape($data);
		
		if($db->get_var("SELECT count(*) FROM {$this->_table} WHERE {$this->_primary} = '{$id}'") >= 1)
		{
			//
			// update session data
			//
			$query = "UPDATE {$this->_table} SET `expires` = {$expires}, `data` = '{$data}' WHERE {$this->_primary} = '{$id}'";
			
			if($db->query($query))
			{
				return true;
			}
		}
		else
		{
			//
			// create a new session row
			//
			$query = "INSERT INTO {$this->_table} (`{$this->_primary}`, `expires`, `data`) VALUES ('{$id}', '{$expires}', '{$data}')";
			
			if($db->query($query))
			{
				return true;
			}
		}
		
		return false;
	}
	
	public function destroy($id)
	{
		//
		// escape user input
		//
		$id = $db->escape($id);
		
		//
		// remove session from database
		//
		$query = "DELETE FROM {$this->_table} WHERE `{$this->_primary}` = '{$id}'";
		
		if($db->query($query))
		{
			return true;
		}
		
		return false;
	}
	
	public function gc($max_lifetime)
	{
		//
		// clean-up all old sessions
		//
		$query = sprintf("DELETE FROM {$this->_table} WHERE expires < %d", time());
		return $db->query($query);
	}
}

?>
Return current item: Icecube