Morgan Green
Morgan Green

Reputation: 996

Welcoming Guest using SQL tables

I have this code currently inside of my page.

<?php
        if ($admin->get_permissions()==3)
        echo 'Welcome Administrator';
        elseif ($admin->get_permissions()==1)
        echo 'Welcome User';
        else
        echo 'Welcome Guest';

        ?>

Now if you're logged in as an admin or a standard user it works fine, but if you're visiting the site as a guest then it gives the error

Call to a member function get_permissions() on a non-object in

The error begins at the start of this script. How could I make sure that the get_permissions function works for people logged in and displays custom data for them and also displays information for a guest logged in? I've been racking my brain around this to no avail. If anybody can help it'd be much appreciated.

Upvotes: 1

Views: 99

Answers (2)

walther
walther

Reputation: 13600

It seems your $admin object isn't created if the user isn't authenticated. To fix this you need to either instantiate the object independently on user authentication or introduce a different check for permissions, maybe something like this:

<?php
     if ($admin != null)
     {
        if ($admin->get_permissions()==3) echo 'Welcome Administrator';
        elseif ($admin->get_permissions()==1) echo 'Welcome User';
     }
     else echo 'Welcome guest.';
?>

Upvotes: 1

alanmanderson
alanmanderson

Reputation: 8200

To me it looks like you don't initiate the $admin variable if the person is a guest. You could us isset() like:

if (!isset($admin)){
    echo 'Welcome Guest';
}
elseif ($admin->get_permissions()==3)
    echo 'Welcome Administrator';
elseif ($admin->get_permissions()==1)
    echo 'Welcome User';

Upvotes: 2

Related Questions