Jump to content

Login and Logout


Knuckles

Recommended Posts

There you go.

 

Logout.php

<?php
if(isset($_COOKIE['cookie_name']) {
  unset($_COOKIE['cookie_name']);
  setcookie('key', '', time() - 3600); 
}
header("Location: http://test.com");
?>

Login.php

<?php

class login
{
	private $init, $db, $cookie = 'cookie_name';
	function login( $init )
	{
		$this->init = $init;
		$this->db = $init->getDB();
		$this->alreadyLogged();
	}
	
	
	 
	 
	function alreadyLogged()
	{
		if(isset($_COOKIE[$this->cookie]) && 
			count(explode(';', base64_decode($_COOKIE[$this->cookie]))) > 3)
		{
			$url = base64_decode($_COOKIE[$this->cookie]);
			if($this->getCookieInformation( $url ))
			{
				header("Location: http://test.test.com");
			} else {
				$this->clearCookie();
			}
		} elseif( isset($_SESSION['login']['SESSIONID'],$_SESSION['login']['FUNCTIE'] ) )
		{
			$row = $this->db->getQfa( 
					$this->db->getQuery("SELECT COUNT(*) as Total 
											FROM Laatst_ingelogd WHERE SessieID = ".
												mysql_escape_string($_SESSION['login']['SESSIONID'])) );
			if($row[0]['Total'] > 0)
			{
				header("Location: http://test.test.com");
			} 
		}
	}
	
	function getCookieInformation( $base )
	{
		$val = $this->getSessieQuery( $base );
		if(mysql_num_rows($val) > 0 && $this->getLoginCredentials() !== NULL)
			return true;
		elseif( mysql_num_rows($val) > 0 )
		{
			$row = $this->db->getQfa( $val );
			$this->setLoginCredentials( $row[0]['SessieID'], $row[0]['Functie'] );
			return true;
		} else
			return false;
	}
	
	function getSessieQuery( $base )
	{
		list($SessieID, $GebruikerID, $Laatst_ingelogd, $IP) = explode(';', $base);
		return $this->db->getQuery("	SELECT li.SessieID, g.Functie
										FROM Laatst_ingelogd li
										INNER JOIN Gebruiker g
										ON li.GebruikerID = g.GebruikerID
										WHERE li.SessieID = ".mysql_escape_string($SessieID)." AND
										li.GebruikerID = ".mysql_escape_string($GebruikerID)." AND
										li.Laatst_ingelogd = '".mysql_escape_string($Laatst_ingelogd)."' AND
										li.IP = '".mysql_escape_string($IP)."'
										LIMIT 1");
	}
	
	function userExists()
	{
		if($_POST)
		{
			if(isset($_POST['emaillogin']) && strlen($_POST['emaillogin']) > 5 && 
				isset($_POST['password']) && strlen($_POST['password']) > 3)
			{
				$sql = sprintf("SELECT GebruikerID 
												   FROM Gebruiker
												   WHERE Email = '".mysql_escape_string($_POST['emaillogin'])."'
												   AND Wachtwoord = '".mysql_escape_string(General::encryptPassword( $_POST['password'] ))."'
												   LIMIT 1");
				
				$query = $this->db->getQuery($sql );
				if(mysql_num_rows($query) > 0)
					return true;
				else
					return false;
			} else
				return false;
		} else
			return false;
	}
	
	function userInformation()
	{
		$val = $this->db->getQfa( $this->db->getQuery("SELECT GebruikerID, Functie
													   FROM Gebruiker
													   WHERE Email = '".mysql_escape_string($_POST['emaillogin'])."'
													   AND Wachtwoord = '".mysql_escape_string(General::encryptPassword( $_POST['password'] ))."'
													   LIMIT 1") );
		return array($val[0]['GebruikerID'], $val[0]['Functie']);
	}
	
	function userLastLogin( $GebruikerID )
	{
		$ip = $_SERVER['REMOTE_ADDR'];
		$timenow = date("Y-m-d H:i:s");
		$this->db->getQuery("INSERT INTO Laatst_ingelogd (GebruikerID, Laatst_ingelogd, IP) 
							VALUES (".$GebruikerID.", '".$timenow."', '".$ip."')");
		return array($this->db->getLastID(), $timenow, $ip);
	}
	
	function setLoginCredentials( $sessie, $Functie )
	{
		$_SESSION['login']['SESSIONID'] = $sessie;
		$_SESSION['login']['FUNCTIE'] = $Functie;
	}
	
	function getLoginCredentials()
	{
		if(isset($_SESSION['login']['SESSIONID'], $_SESSION['login']['FUNCTIE']))
			return array( $_SESSION['login']['SESSIONID'], $_SESSION['login']['FUNCTIE'] );
		else
			return NULL;
	}
	
	function setCookie( $GebruikerID, $Functie )
	{
		list($SessieID,$Laatst_ingelogd,$IP) = $this->userLastLogin( $GebruikerID );
		$encoded = base64_encode( $SessieID.';'.$GebruikerID.';'.$Laatst_ingelogd.';'.$IP );
		$howlong = time() + 126144000; //4 years
		setcookie( $this->cookie, $encoded, $howlong, '/');
		$this->setLoginCredentials( $SessieID, $Functie );
	}
	
	function clearCookie()
	{
		$howlong = time() - 126144000; //4 years
		setcookie( 'cookie_name', "", $howlong);
	}
	
	function justlogin()
	{
		if($this->userExists())
		{
			list($GebruikerID, $Functie) = $this->userInformation();
			list($SessieID,$Laatst_ingelogd,$IP) = $this->userLastLogin( $GebruikerID );
			$this->setLoginCredentials( $SessieID, $Functie );
			return true;
		} else
			return false;
	}
	
	function keepLoggedin()
	{
		if($this->userExists())
		{
			list($GebruikerID, $Functie) = $this->userInformation();
			$this->setCookie( $GebruikerID, $Functie );
			return true;
		} else
			return false;
	}
}
/*
$this->setAssign('errEmail', '');
$this->setAssign('errPassword', '');

$login = new login( $this );
if($_POST)
{
	if(isset($_POST['keeploggedin']) && $_POST['keeploggedin'] == 1) {
		if($login->keepLoggedin()){
			header("Location: http://test.test.com");
		} else {
			$this->setAssign('errEmail', 'rood');
			$this->setAssign('errPassword', 'rood');
		}
	} else {
		if($login->justlogin()) {
			header("Location: http://test.test.com");
		} else {
			$this->setAssign('errEmail', 'rood');
			$this->setAssign('errPassword', 'rood');
		}
	}
}*/

?>
Link to comment
https://forums.phpfreaks.com/topic/276120-login-and-logout/#findComment-1420904
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.