Jump to content

Remember Me Headache [solved]


nick89

Recommended Posts

It's been quite a while since I posted here, and the reason is I can usually sort my problems out. But this has really stumped me.

The script has a global header file 'header.php', which is included into each of the other php files. This header file handles the session and does a bunch of other stuff which we like to do at the start of each page.

I recently set about modifying this header to incorporate a RememberMe function, and I've managed to create a bug which I can't solve.

the header looks a bit like this:

[code]<?php

session_start();

include 'config.php';
include 'dbconfig.php';
include 'remember_me.php'; //Just a library of functions: CheckRemember(), RememberMe(), ForgetMe().

if($_SESSION['loggedin'])
//Whatever
elseif(CheckRemember())
//Reload all those session variables
else
//Set as guest

//Do other things
?>[/code]

Now here's what I've found so far:
[list]
[*]The script will crash on the CheckRemember() line, unless there is an existing session (regardless of the value of $_SESSION['loggedin']) or if there is no Remember Cookie set.
[*]If I create a test script just including the database config and the remember_me functions in which session_start is never called, I can get CheckRemember() working fine.
[/list]

Any thoughts?
Link to comment
Share on other sites

Sure.

[code]<?php

function CheckRemember()
{
global $db;

if(!isset($_COOKIE['RememberCookie']))
return false;

$rCookie = explode('-', $_COOKIE['RememberCookie']);

if(count($rCookie) != 2)
return false;

$rCookie[0] = intval($rCookie[0]);

//Cookie is uid-hash
if(!$db->qry("SELECT * FROM users WHERE uid = {$rCookie[0]}"))
return false;

if($db->rowcount() == 0)
return false;

$targetUser = $db->fetch(false);

if($rCookie[1] == md5($targetUser['username'].$targetUser['password']))
return $targetUser;
else
return false;
}

function RememberMe($who)
{
global $db;

$who = intval($who);

$db->qry("SELECT * FROM users WHERE uid = $who");
$targetUser = $easydb->fetch(false);

return setcookie('RememberCookie', $who.'-'.md5($targetUser['username'].$targetUser['password']), time() + 14515200);
}

function ForgetMe()
{
return setcookie('RememberCookie', 'FORGET', time() + 14515200);
}

?>[/code]
Link to comment
Share on other sites

Looks like there is a bug in CheckRemember().

If possible, enable displaying of php errors.

If you can't do that (or if it's not convenient), than add some print statements throughout CheckRemember(), and see how many of them are displayed before it fails.  That will help you narrow down where the problem is.  I don't notice anything wrong right now, but the symptoms you described tell me there is an error there somewhere.
Link to comment
Share on other sites

the error is in the

[code]if(!$db->qry("SELECT * FROM users WHERE uid = {$rCookie[0]}"))
return false;[/code]

lines, but what surprises me is that this function has worked for me in that test. Furthermore, I can't see anything wrong with that query, and I know this database fairly well.
Link to comment
Share on other sites

That leaves only one possibility.. the $db object is not behaving like it's supposed to.  If the crash occurs during that query, but the $db object should return false on failure, then the $db object must be buggy (or being used incorrectly).  So.. what's in dbconfig.php? :)
Link to comment
Share on other sites

You're right to a degree. It seems to be a problem with scope (and I still can't fix it!)

[code]global $db;[/code]

I added some code after here which says:
[code]if(!isset($db)) echo 'BAD';[/code]

And what do you know, it echoes BAD.

But it gets more confusing - I did a test straight after the include of dbconfig, and it tells me my $db object exists!

So why isn't the global keyword giving me access to my $db object?!?!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.