Location: PHPKode > scripts > EZ Framework > ez-framework/auth.php
<?php
# Version 1.0

class auth
{
	var $user = array();
	var $session;
	var $error = '';
	
	//Generate a random session/cookie value
	function genRand()
	{
		$session_rand = substr(crypt($this->user['login']),0,10) . 
						substr($this->user['login'],1,1) .
						substr(md5(time()),0,22) .
						substr($this->user['login'],0,1) .
						substr(time(),6,4);
		$this->session_rand = $session_rand;
		return $session_rand;
	}
	
	# Retreive Variables
	function getError($code = '')
	{
		//Return error array
		if(empty($code))
			return $this->error;
		//Return single error based on given code
		else
			return $this->error[$code];
	}
	
	#Try to log user in, returns true on success, false if user is not valid
	//If you want to use a cookie set the 3rd parameter to 1 and the 
	//4th to how long to cookie will last in hours
	function login($login,$pass,$cookie = false,$cookie_time_hr = 24)
	{
		global $sql;
		global $_T;
		global $_AUTH;
		//Try to select user based on name and password
		$sql->go("SELECT * FROM  {$_T['USER']} WHERE {$_AUTH['USER']['login']} = '$login' AND {$_AUTH['USER']['password']} = '$pass' LIMIT 1",'login');
		//Check to see if the user exsist
		if($sql->numRows('login') == 1)
		{
			$row = $sql->fetchArray('login');
			$this->user = $row;
			unset($row);
			//generate random session value
			$session_rand = auth::genRand();
			$this->session = $session_rand;
			//Update the users session value
			$sql->go("INSERT INTO {$_T['SESSION']} 
					({$_AUTH['USER']['id']},{$_AUTH['SESSION']['id']},{$_AUTH['SESSION']['begin']},{$_AUTH['SESSION']['end']},{$_AUTH['SESSION']['ip']}) 
					VALUES 
					('{$this->user['user_id']}','$session_rand','".time()."','".time()."','{$_SERVER['REMOTE_ADDR']}')");

			//If using a cookie
			if($cookie)
			{
				$length = ($cookie_time_hr * 60 * 60);
				if(!setcookie(ID_SESSION_NAME,$session_rand,time()+$length))
				{
					$this->error['set_cookie'] = 'Unable to set cookie, a session was used instead';
					$_SESSION[ID_SESSION_NAME] = $session_rand;
				}
			//If not using a cookie
			}else
			{
				$_SESSION[ID_SESSION_NAME] = $session_rand;
			}
			//user has been logged in
			return true;
		}else
		{
			//user failed login authentication
			return false;
		}
	}

	function isUser()
	{
		global $sql;
		global $_T;
		global $_AUTH;
		//check to see if a cookie or session is already set
		if(isset($_COOKIE[ID_SESSION_NAME]))
		{
			$select = $_COOKIE[ID_SESSION_NAME];
		}else
		{
			$select = $_SESSION[ID_SESSION_NAME];
		}
		
		//select from database based on the cookie or session value
		$sql->go("SELECT {$_T['USER']}.* ,{$_T['SESSION']}.*
				  FROM {$_T['USER']}
				  LEFT JOIN {$_T['SESSION']} ON ({$_T['USER']}.{$_AUTH['USER']['id']} = {$_T['SESSION']}.{$_AUTH['SESSION']['user_id']})
				  WHERE {$_T['SESSION']}.{$_AUTH['SESSION']['id']} = '$select' AND 
				  {$_T['SESSION']}.{$_AUTH['SESSION']['ip']} = '{$_SERVER['REMOTE_ADDR']}' 
				  ORDER BY {$_AUTH['SESSION']['begin']} DESC LIMIT 1 ",'is_user');
		//user passes check
		if($sql->numRows('is_user') == 1)
			return true;
		else
			return false;
	}
	//Check to see if a user is already logged in
	//Returns true on success false if user is not already logged in
	//This checks against cookies and sessions, whichever is found if any
	function check()
	{
		global $sql;
		global $_T;
		global $_AUTH;
		if(auth::isUser())
		{
			//get and set user information
			$row = $sql->fetchArray('is_user');
			$this->user = $row;
			unset($row);
			$cur_time = time();
			
			$elapsed_time = $cur_time - $this->user[$_AUTH['SESSION']['end']];
			$total_session_time = $this->user[$_AUTH['SESSION']['total']] + $elapsed_time;
			// They have to be active within 10 mins to get a totaltime increase
			if( $elapsed_time > 600 )
				$total_session_time = $this->user[$_AUTH['SESSION']['total']];
			$sql->go("UPDATE {$_T['SESSION']}
					  SET {$_AUTH['SESSION']['total']} = '$total_session_time',
						  {$_AUTH['SESSION']['end']} = '$cur_time'
					  WHERE {$_AUTH['SESSION']['id']} = '{$this->user[$_AUTH['SESSION']['id']]}' LIMIT 1",'update_total_session_time');
			return true;
		}else
			return false;
		
	}
	function updateCookie($cookie_time_hr = 24)
	{
		if(isset($_COOKIE[ID_SESSION_NAME]))
		{
			$length = ($cookie_time_hr * 60 * 60);
			
			setcookie(ID_SESSION_NAME,$this->session,time() + $length);
		}
	}

	//logout a user
	function logout()
	{
		global $sql;
		global $_T;
		global $_AUTH;
		$sql->go("UPDATE {$_T['SESSION']}
				  SET {$_AUTH['SESSION']['end']} = '".time()."'
				  WHERE {$_AUTH['SESSION']['id']} = '{$this->user['session_id']}' LIMIT 1",'logout');
		//Remove cookie if found
		if(isset($_COOKIE[ID_SESSION_NAME]))
			@setcookie(ID_SESSION_NAME,'-1',time()-60);
		
		//Destroy session if found
		@session_unset();//Clear all session variables.
		@session_destroy();//Destroy the session.
		$_SESSION = array();
		$_COOKIE = array();

	}
	//Display variables in class along with cookie and session super global arrays
	function debug()
	{
		echo '<br>';
		print_r($this->user);
		echo '$error: '; 		print_r($this->error); echo'<br>';
		echo '$_COOKIE: '; 		print_r($_COOKIE); echo'<br>';
		echo '$_SESSION: '; 	print_r($_SESSION); echo'<br>';
	}
}
?>
Return current item: EZ Framework