Jump to content

[SOLVED] Problems with registering sessions from cookies


AdRock

Recommended Posts

I am having a problem with ,y login system

 

When i login sessions are registered and if the 'remember me' box is ticked, some cookies are set too.

 

The problem is when I come back to the page, I should be logged in automatically becuase I create new sessions with the values in the cookies.

 

When i try to print_r the cookies and sessions when i first open the page after logging in and setting the cookies, all the cookies values are displayed but not the sessions, so somewhere the sessions are not getting registered with the cookie values.

 

When the page loads, the checklogin() function is called which should check is some cookies are registered and if they are create the session.

 

here is my code

 

function confirmuser() {
global $host,$dbUser,$dbPass,$dbName;

require_once("php/database/connection.php");
require_once("php/database/MySQL.php");

// Connect to the database and grab the email
$db = & new MySQL($host,$dbUser,$dbPass,$dbName);

$user = $_SESSION['encrypted_name'];
$email = $_SESSION['ecrypted_email'];
$pass = $_SESSION['encrypted_pass'];

    // Try and get the user using the username or email and encrypted pass
    $sql= "SELECT userid, username, email, user_level FROM users WHERE md5(username)='$user' OR md5(email)='$email') AND password='$pass' LIMIT 1";

    $result = $db->query($sql);
    $numrows = $result->size();

$items = array();
while ($row = $result->fetch()) {
	$items = array(
		'user_id' => $row['userid'],
		'username' => $row['username'],
		'email' => $row['email'],
		'user_level' => $row['user_level'],	
		'encrypted_id' => md5($row['userid']),
		'encrypted_name' => md5($row['username']),
		'encrypted_email' => md5($row['email']),
		'encrypted_user' => md5($row['user_level'])
		);
}
$session_array = array(
	$_SESSION['userid'],
	$_SESSION['username'],
	$_SESSION['password'],
	$_SESSION['email'],
	$_SESSION['encrypted_id'],
	$_SESSION['encrypted_name'],
	$_SESSION['encrypted_email'],
	$_SESSION['encrypted_user']
);

foreach($session_array as $key) {
	if(strcmp($key, $items[$key]) != 0) {
		return false;
	}
}
}

function checkLogin(){
if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookemail'])){
	$_SESSION['userid'] = $_COOKIE['cookid'];
    	$_SESSION['username'] = $_COOKIE['cookname'];
    	$_SESSION['email'] = $_COOKIE['cookemail'];
    	$_SESSION['user_level'] = $_COOKIE['cookuser'];
    	$_SESSION['encrypted_id'] = $_COOKIE['cookencid'];
    	$_SESSION['encrypted_name'] = $_COOKIE['cookencname'];
	$_SESSION['encrypted_pass'] = $_COOKIE['cookencpass'];
    	$_SESSION['encrypted_email'] = $_COOKIE['cookencemail'];
    	$_SESSION['encrypted_user'] = $_COOKIE['cookencuser'];
}

/* Username and password have been set */
if(isset($_SESSION['name']) && isset($_SESSION['pass'])){
	/* Confirm that username and password are valid */
	if(confirmUser($_SESSION['userid'], $_SESSION['username'], $_SESSION['email'],$_SESSION['user_level'],
		$_SESSION['encrypted_id'], $_SESSION['encrypted_name'], $_SESSION['encrypted_pass'], 
		$_SESSION['encrypted_email'], $_SESSION['encrypted_user']) != 0) {

		unset($_SESSION['userid']);
		unset($_SESSION['username']);
		unset($_SESSION['password']);
		unset($_SESSION['username']);
		unset($_SESSION['email']);
		unset($_SESSION['encrypted_id']);
		unset($_SESSION['encrypted_name']);
		unset($_SESSION['encrypted_pass']);
		unset($_SESSION['encrypted_email']);
		unset($_SESSION['encrypted_user']);
		return false;
	}
	return true;
}
/* User not logged in */
else{
	return false;
}
}

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.