注意:尝试在php中获取非对象的属性[重复]

This question already has an answer here:

I have a problem in userpie login and register script. So the script works good using lamp on linux mint! But when I go on windows there is more than 15 problem, one of them is

Notice: Trying to get property of non-object in C:\armadicehost\htdocs\models\funcs.user.php on line 164

Notice: Trying to get property of non-object in C:\armadicehost\htdocs\models\funcs.user.php on line 165

This is the code that line 165 and 164 have:

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)."'
                AND password = '".$db->sql_escape($loggedInUser->hash_pw)."' 
            AND
            active = 1
            LIMIT 1";
            }

    if($loggedInUser == NULL)

And for login.php it says...

Notice: Undefined index: status in C:\armadicehost\htdocs\login.php on line 401

    <?php
    if(!empty($_POST))
    {
    ?>
    <?php
    if(count($errors) > 0)
    {
    ?>

    <?php
            } }
    ?> 

I need to fix this please any idea about them?

</div>

It seems that due to your logic, $loggedInUser is not initialized properly. "non-object" in that case means that it is either null or not an object (i.e. it is a string or a number ...).

Try to change:

function isUserLoggedIn()
{
    global $loggedInUser,$db,$db_table_prefix;

    if ( !isset($loggedInUser) || !isset($db) || !isset($db_table_prefix) )
    {
         throw new Exception "Objects and/or globals not initialized.";
    }

    $sql = "SELECT user_id,
    .....

This will terminate your script if things are going wrong. Since the rest of code is missing, it's almost impossible to tell where exactly the problem resides.

To your second Q: this seems not to be the proper code since there is no array reference with an index 'status'

Like Axel mentioned in his answer, $loggedInUser is definitely the problem. I do not agree with Axel's solution though.

Because the goal of the method is to check wether or not the user is loggedin, it should probably return TRUE of FALSE based on whether the user is logged in or not. If the user is not loggedin, $loggedInUser won't be set, in which case you should return false.

function isUserLoggedIn()
{
    global $loggedInUser,$db,$db_table_prefix;

    if (!is_object($loggedInUser)) {
        return false;
    }

    ...
}

Notice: Trying to get property of non-object in...

Solution:

Check for global variables initizlization before usage:

function isUserLoggedIn()
{
    global $loggedInUser,$db,$db_table_prefix;

    if (!isset(
        $db,
        $db_table_prefix,
        $loggedInUser->user_id,
        $loggedInUser->hash_pw
    )) {
        // Handle non-initizlized variables case
    }

    $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";
}

FYI

It can be dangerous to use sql-queries concatenation in this way:

$sql = "SELECT user_id,
              password
            FROM ".$db_table_prefix."users  <--- POSSIBLE SQL-INJECTION
....

Sanitize $db_table_prefix before concatenation.


Notice: Undefined index: status in...

Tis notice tells that was used not defined array element with index status. Unfortunately your second code example haven't clue for what exactly happend there.

Solution:

Check for array element initialization before usage:

if (!isset($array['status']) {
    // Handle non-initialized array element case
}