How do I resolve this php error
I am using usercake for user management module in my project. This is my first PHP project and I am still learning. I setup the database and usercake project but I keep getting this error -
Notice: Trying to get property of non-object in C:\wamp\www\userCake\models\funcs.user.php on line 170
Notice: Trying to get property of non-object in C:\wamp\www\userCake\models\funcs.user.php on line 172
And this error is caught only when there is no user logged in to the system. Once, a user logs in I no longer get this error. The code responsible is -
function isUserLoggedIn()
{
global $loggedInUser,$db,$db_table_prefix;
$sql = "SELECT User_ID,
Password
FROM ".$db_table_prefix."Users
WHERE
User_ID = '".$db->sql_escape($loggedInUser->user_id)."' //line 170
AND
Password = '".$db->sql_escape($loggedInUser->hash_pw)."' // line 172
AND
Active = 1
LIMIT 1";
$sql = mysql_num_rows($sql);
if($loggedInUser == NULL)
{
return false;
}
else
{
//Query the database to ensure they haven't been removed or possibly banned?
if(returns_result($sql) > 0)
{
return true;
}
else
{
//No result returned kill the user session, user banned or deleted
$loggedInUser->userLogOut();
return false;
}
}
}
I tried to fix this with my limited knowledge of php but wasn't able to. I guess this error is similar to Object reference not set to an instance of an object in .net. Am I right? If 开发者_开发知识库not, what is causing this error?
How can I fix this?
I guess the first time a User is not logged in, the reference is null, so in line 170, $db->sql_escape($loggedInUser->user_id).
is causing something like a null reference. Try moving the
if($loggedInUser == NULL)
{
return false;
}
in the beginning of the code to stop dereferencing if the user is null.
function isUserLoggedIn() {
global $loggedInUser, $db, $db_table_prefix;
if ($loggedInUser == NULL) { return false; }
else {
$sql = "SELECT User_ID,
Password
FROM " . $db_table_prefix . "Users
WHERE
User_ID = '" . $db->sql_escape($loggedInUser->user_id) . "'
AND
Password = '" . $db->sql_escape($loggedInUser->hash_pw) . "'
AND
Active = 1
LIMIT 1";
//Query the database to ensure they haven't been removed or possibly banned?
if (returns_result($sql) > 0) { return true; }
else {
//No result returned kill the user session, user banned or deleted
$loggedInUser->userLogOut();
return false;
}
}
}
Replace the function with this. The sql statement shouldnt be set until after it checks if $loggedInUser is null ..
I had the exact same problem,
It seems i had my own database connection whice was initiated after the userCake connection was made, this obviously overides userCake connection making it impossible for it sql_escape.
try to disable any database connection after the userCake inclusion.
BTW you should also remove any session_start() before userCake inclusion. this will cause __php_incomplete_class error in the session
I suspect the global variable $loggedInUser
is null as long as the user is not logged in. So using it through SQL to detect if the user is logged in... It's a snake biting its own tail.
You probably want to return false earlier if $loggedInUser
is null.
精彩评论