nick89 Posted November 17, 2006 Share Posted November 17, 2006 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? Quote Link to comment Share on other sites More sharing options...
doni49 Posted November 17, 2006 Share Posted November 17, 2006 Can you post the code from remember_me.php? Quote Link to comment Share on other sites More sharing options...
nick89 Posted November 17, 2006 Author Share Posted November 17, 2006 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] Quote Link to comment Share on other sites More sharing options...
nick89 Posted November 17, 2006 Author Share Posted November 17, 2006 anyone? Quote Link to comment Share on other sites More sharing options...
btherl Posted November 17, 2006 Share Posted November 17, 2006 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. Quote Link to comment Share on other sites More sharing options...
nick89 Posted November 17, 2006 Author Share Posted November 17, 2006 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. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 17, 2006 Share Posted November 17, 2006 Does the query fail? $rCookie[0] was forced to be an integer, so I don't see how a syntax error is possible. The only problems I can imagine is if the uid column doesn't exist, or if no rows are returned from the query. Quote Link to comment Share on other sites More sharing options...
nick89 Posted November 17, 2006 Author Share Posted November 17, 2006 Returning a result of zero will not cause it to crash.And the uid column definitely exists.If the query failed the result object would be false and the function would return false.Any more ideas? Quote Link to comment Share on other sites More sharing options...
btherl Posted November 17, 2006 Share Posted November 17, 2006 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? :) Quote Link to comment Share on other sites More sharing options...
nick89 Posted November 17, 2006 Author Share Posted November 17, 2006 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?!?! Quote Link to comment Share on other sites More sharing options...
btherl Posted November 17, 2006 Share Posted November 17, 2006 According to my understanding of scope, it should give you access..Is $db set right before you call CheckRemember()? Quote Link to comment Share on other sites More sharing options...
nick89 Posted November 17, 2006 Author Share Posted November 17, 2006 I found a work around. I passed $db via reference and it worked. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.