<?

class mysession 
	{
	//MYSQL VARS
	var $mysqlhost;		//mysql server adress
	var $mysqluser;		//mysql server user
	var $mysqlpass;		//mysql server password
	var $mysqldb;		//mysql database
	
	//COOKIE VARS
	var $website;		//website name
	var $path;			//cookiepath, leave empty
	var $domain;		//website domain
	var $expire;		//cookie expire time in seconds
	
	//CONFIG VAR
	var $defaultpath;	//Default path after logout/failed login
	
	function SetVars ($mysql,$cookie,$config) 
		{
		//MYSQL VARS
     	$this->mysqlhost=$mysql['host'];
     	$this->mysqluser=$mysql['user'];
     	$this->mysqlpass=$mysql['pass'];
     	$this->mysqldb=$mysql['db'];
		//COOKIE VARS
		$this->website=$cookie['website'];
		$this->path=$cookie['path'];
     	$this->domain=$cookie['domain'];
     	$this->expire=$cookie['expire'];
		//CONFIG VARS
		$this->defaultpath=$config['path'];
        }
			
	function db_connect()
		{
		$conn = mysql_connect($this->mysqlhost,$this->mysqluser,$this->mysqlpass) or die ("Unable to connect to database");
		$db   = mysql_select_db($this->mysqldb,$conn) or die ("Unable to connect to database");
		}
	
	function createtable()
		{
		$sql =
			'
			CREATE TABLE `mysession` 
				( 
				`id` int(4) NOT NULL auto_increment, 
				`name` varchar(20) NOT NULL default \'\', 
				`password` varchar(32) NOT NULL default \'\', 
				`timehash` varchar(32) NOT NULL default \'\', 
				`ip` varchar(20) NOT NULL default \'\',
				`firstname` varchar(50) NOT NULL default \'\',
				`surname` varchar(20) NOT NULL default \'\', 
				`email` varchar(50) NOT NULL default \'\', 
				PRIMARY KEY  (`id`) 
				) 
			TYPE=MyISAM AUTO_INCREMENT=1			
			';
		$return = mysql_query ($sql);
		}
		
	function login($name, $pass)
		{
		$sql = 'SELECT * FROM mysession WHERE name="'.$name.'" AND password="'.md5($pass).'"'; 
		$rows = mysql_num_rows(mysql_query($sql)); 
		if($rows == 1)  
			{
			$timehash = md5(time()); 
			$update = 'UPDATE mysession SET timehash="'.$timehash.'", ip="'.$_SERVER[REMOTE_ADDR].'" WHERE name="'.$name.'"'; 
			mysql_query($update); 
			setcookie($this->website, $timehash, time()+$this->expire, "", $this->domain, "0"); 
			} 
		if($rows == 0) 
			{ 
			echo '<script>location.href="'.$this->defaultpath.'"</script>';
			}
		}
	
	function check()
		{
		global $name;
		global $pass;
		$sql = 'SELECT * FROM mysession WHERE timehash="'.$_COOKIE[$this->website].'" AND ip="'.$_SERVER[REMOTE_ADDR].'"'; 
		$select = mysql_query($sql); 
		$rows = mysql_num_rows($select); 
		if($rows > 0)  
			{ 
			$user = mysql_fetch_array($select); 
			$timehash = md5(time()); 
			$update = 'UPDATE mysession SET timehash="'.$timehash.'" WHERE ip="'.$_SERVER[REMOTE_ADDR].'"'; 
			mysql_query($update); 
			setcookie($this->website, $timehash, time()+$this->expire, "", $this->domain, "0"); 
			return $user; 
			}
		} 
		
	function logout()
		{
		$update = 'UPDATE mysession SET timehash="'.md5(time()).'" WHERE ip="'.$_SERVER[REMOTE_ADDR].'" AND timehash="'.$_COOKIE[$this->website].'"'; 
		mysql_query($update); 
		echo '<script>location.href="'.$this->defaultpath.'"</script>';
		} 
	}
?>