This is the full session class.
/*
* Cabbit 1.0
* Licence: GPLv2
* CabbitSession v1.2
*/
class CabbitSession
{
/*
* Variables
*/
public $username;
public $password;
public $errorMessages;
public $killSession;
public $userID;
public function Login()
{
if ($this->processLogin() == true)
{
header('Location: /'); // Sent user to home page
exit();
}
}
public function returnErrors()
{
if ($this->processLogin() == false)
{
$this->errorMessages = array("loginValidate" => 'Username or Password is invalid.');
}
return $this->errorMessages;
}
public function userSession()
{
if (isset($_COOKIE['SessionID']))
{
global $DatabaseTable;
// gets the username from cookie
$userid = $_COOKIE['SessionID'];
// connect to user table
/* Connect to the table */
$task = "dbmysql";
$query_select = "select `id`, `userID`, `userName`, `password` from `users`";
$database_name = $DatabaseTable;
/* Initialise the database */
$dbC = new DbControl($task);
$dbC->selectDb($database_name);
$dbC->setQuery($query_select);
$dbR = $dbC->initiate();
// Cycles though the results to see if the user exists //
while ($dbR->next())
{
// checks the cookie username with the database //
if ($dbR->get("userID") == $userid)
{
$_SESSION['userID'] = $dbR->get("userID");
return true;
}
}
}
}
protected function processLogin()
{
// Username and password
$username = $this->username;
$password = $this->password;
global $DatabaseTable;
/* Connect to the table */
$task = "dbmysql";
$query_select = "select `userName`, `password` from `users` WHERE `userName` = '".$username."'";
$database_name = $DatabaseTable;
/* Initialise the database */
$dbConnect = new DbControl($task);
$dbConnect->selectDb($database_name);
$dbConnect->setQuery($query_select);
$dbObject = $dbConnect->initiate();
if ($dbObject->get("userName") == $username AND $dbObject->get("password") == md5($password))
{
/*
Function gives the user a new unique id.
*/
$setID = md5(rand(0, 128)); //Generates a new id for the user's session
$task = "dbmysql";
$dbConnect = new DbControl($task);
$dbConnect->selectDb($DatabaseTable);
$dbConnect->setQuery("UPDATE `users` SET `userID` = '$setID' where `userName` = '$username'");
$dbConnect->initiate();
setcookie("SessionID", $setID, time()+10800, "/");
return true;
}
else
{
return false;
}
}
public function logout()
{
if ($this->killSession == true)
{
setcookie("SessionID", "", time()+0, "/");
session_destroy();
header('Location: /'); // Sent user to home page
exit();
}
}
}