简体   繁体   中英

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?

Replace this code:

if(logged_in() === true)

With this code:

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

That cuts out all the middlemen.

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.

如果您完全确定声明包含在每个页面中,那么我建议您检查一下以确保函数未在对象内部声明为方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM