shohamh
shohamh

Reputation: 317

Fatal error: Call to undefined function logged_in()

I made a login script, when I want to check when the user is logged in, I use the function logged_in(), which consists of:

function logged_in()
{
    if(isset($_SESSION['id']))
    {
        return true;
    }
    else 
    {
        return false;
    }
}

the session is set here:

else if ($login === true)
        {
            echo 'Login success.';
            include 'include/aside.php';
            include 'include/footer.php';
            $userid = id_from_username($username);
            $usernameforsession = username_from_id($userid);
            $_SESSION['id'] = $userid;
            $_SESSION['username'] = $usernameforsession;
            header( "refresh:2;url=index.php");
            exit();
        }

And this is an example of me using it in 'index.php':

if(logged_in() === true)
{
    echo 'Site content when user is logged in.';
} else if(logged_in() === false)
{
    include 'include/widgets/register.php'; //registration form
}

And yes, the function is included in every page. I made this function so it should work... Why isn't it working?

Upvotes: 0

Views: 5403

Answers (3)

dotancohen
dotancohen

Reputation: 31471

Replace this code:

if(logged_in() === true)

With this code:

if(isset($_SESSION['id']))

That cuts out all the middlemen.

Upvotes: 1

Makita
Makita

Reputation: 1812

If you are absolutely sure that the declaration is being included on every page, then I would suggest that you check to make sure the function is not declared as a method inside an object.

Upvotes: 0

Fluffeh
Fluffeh

Reputation: 33502

Although you have listed quite a bit of code, are you sure you are including session_start(); at the top of each page? You need to call this before you do anything at all with the session variables.

The second thing is that the error message is showing that the function isn't defined - are you sure you have it either in the source for the page or as an include to the code that defines it on each page?

If you have it in a file called 'functs.php', you need to include() it in every page that will make a call to that function.

Upvotes: 0

Related Questions