Location: PHPKode > scripts > New DB Class > new-db-class/session_handler.php
<?php
/* vim: set ts=2 sw=2: */
// +----------------------------------------------------------------------+
// | PHP version 4.0                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002 The PHP Group       |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | hide@address.com so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Ji&#345;í Kocman <hide@address.com>                          |
// +----------------------------------------------------------------------+

/**
* class session handler
* @package db_class
* @author Ji&#345;í Kocman - hide@address.com
* @version $Id: session_handler.php,v 1.1 2003/02/04 00:03:08 jirka Exp $
*/

class sess_handler extends db
{
	var $db_Table;							// tabulka sessions
	var $sess_id;								// id session
	var $sess_expire_time;			// doba platnosti
	var $sess_data = Array();		// data session
	var $cookie;								// zapnute/vypnute cookies

	/**
	* konstruktor da tride nejaky hodnoty. pokud neni sess_id tak vygeneruje
	*/
	function sess_handler($db_Table, $sess_expire_time, $server, $user, $pass, $new, $db = NULL, $sess_id = NULL, $cookie = 0)
	{
		// inicializace
		$this->db_Table					= $db_Table;
		$this->cookie						= $cookie;
		$this->sess_expire_time = $sess_expire_time;
		$this->db_Connect($server, $user, $pass, $new);
		if ($db)
		{
			$this->db_Select_Db($db);
		}

		if ($sess_id == NULL)
		{
			$this->create();
		}
		else 
		{
			$this->db_query('SELECT data, UNIX_TIMESTAMP(time) FROM ' . $this->db_Table . ' WHERE sess_id = "' . $sess_id . '" AND ip = "' . $_SERVER["REMOTE_ADDR"] .'"');
			if ($this->db_num_rows($this->SQL) == 0)
			{
				$this->create();
			}
			else 
			{
				if ((($this->db_result(0,1,$this->SQL) + $this->sess_expire_time) <= time()) && ($this->sess_expire_time > 0))
				{
					$this->create();
				}
				else 
				{
					$this->sess_id = $sess_id;
					$this->sess_data = unserialize(base64_decode($this->db_result(0,0,$this->SQL)));
					$this->db_query('UPDATE ' . $this->db_Table . ' SET ip = "' . $_SERVER["REMOTE_ADDR"] . '", time = NOW() WHERE sess_id = "' . $this->sess_id . '"');
					if ($this->cookie == 1)
					{
						if ($this->sess_expire_time > 0)
						{
							setcookie('SID', $this->sess_id, time() + $this->sess_expire_time);
						}
						else
						{
							setcookie('SID', $this->sess_id, time() + 31536000);
						}
					}
				}
			}
		}
	}

	/**
	* funkce vytvori session
	*/
	function create()
	{
		$this->sess_id = md5(uniqid(getmypid() . rand(),1));
		$this->db_query('INSERT INTO ' . $this->db_Table . ' SET sess_id = "' . $this->sess_id . '", ip = "' . $_SERVER["REMOTE_ADDR"] . '", time = NOW()', $this->CONN);
		if ($this->cookie == 1)
		{
			setcookie('sess_id', $this->sess_id, time() + $this->sess_expire_time);
		}
		return;
	}

	/**
	* funkce vraci sess_id
	*/
	function get_id()
	{
		return $this->sess_id;
	}

	/**
	* funkce maze data
	*/
	function delete()
	{
		$this->sess_data = array();
		return;
	}
	
	/**
	* funkce maze obsah indexu
	*/
	function drop($key)
	{
		unset($this->sess_data[$key]);
		return;
	}

	/**
	* funkce uklada session
	*/
	function save()
	{
		$data = base64_encode(serialize($this->sess_data));
		$this->db_query('UPDATE ' . $this->db_Table . ' SET data = "' . $data . '" WHERE sess_id = "' . $this->sess_id . '"');
		$this->db_Close();
		return;
	}

	/**
	* funkce uklada session na disk
	*/
	function load()
	{
		return $this->sess_data;
	}
}
?>
Return current item: New DB Class