0

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?

Community
  • 1
  • 1
mykokohat
  • 9
  • 6
  • http://userpie.com/ thats the framwork that i use and it have the problem on xampp using the same script on linux and it works ! – mykokohat Oct 19 '14 at 20:29

3 Answers3

0

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'

Axel Amthor
  • 10,816
  • 1
  • 24
  • 44
  • Like you said yourself, $loggedIn user might have been set, but may be something other than an object. In that case !isset() won't do. So I'd go with !is_object() in this case. – vrijdenker Oct 19 '14 at 16:05
  • thats the full script that i use and it have the problem userpie.com – mykokohat Oct 19 '14 at 22:23
  • @mykokohat won't you please do not blame us for the errors in *your* code ... – Axel Amthor Oct 19 '14 at 22:46
  • im not blaming any one man im just asking :D tring to find out why it happin and how i could fix it ! its not my code im not a proggramer :D just tring to learn and still reading the fundementals of php :/ sorry if u feel i was blaming you or any once else but im not :D thats the good thing – mykokohat Oct 19 '14 at 22:59
  • just my 2 cents: I won't use that userpie.com stuff. It's badly documented and there's no meaningful intro on how to use it. Do it on your own and you will learn things. – Axel Amthor Oct 19 '14 at 23:03
  • by the way after i used the code the you put up " after 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."; } it says Parse error: syntax error, unexpected '"Objects and/or globals not in' (T_CONSTANT_ENCAPSED_STRING) in C:\armadicehost\htdocs\models\funcs.user.php on line 165 – mykokohat Oct 19 '14 at 23:06
  • not realy sure if its a good thing or a bad thing :D :D as i said i dont realy understand how stuff works yet man :D – mykokohat Oct 19 '14 at 23:08
0

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

    ...
}
vrijdenker
  • 1,343
  • 12
  • 25
  • thats the full script that i use and it have the problem http://userpie.com/ – mykokohat Oct 19 '14 at 20:27
  • wouldn't that the be `if ( $loggedInUser === false ) ...`? But according to the poorly documented so called framweork I thing it's a class with a constructor which can't hardly be unset, unless it's not instantiated. Since we do not see the rest of the code (where the global is used elsewhere ...) it's a guess anyway. – Axel Amthor Oct 19 '14 at 22:45
0

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
}
Alexander Yancharuk
  • 12,892
  • 5
  • 49
  • 54