Jump to content

$_SESSION Variable checking does not work...


migi0027

Recommended Posts

<?php
function Login()
{
    session_start();
$_SESSION["loggedin"] = true;
echo "You are logged in!";
    header("Location: ../L_index.php"); //Aka Logged in Index.
}

function Logout()
{
$_SESSION["loggedin"] = false;
    session_destroy();
}

function checklogin()
{
    session_start();
    if ($_SESSION["loggedin"] == true)
    {
        return true;
    }
    else
    {
        return false;
    }
}
?>

 

This is just a tiny bit of my code, there is security holes, but as you see, there is 3 functions:

Login

Logout

Checklogin

 

But when i use the Login function, everything is fine, but if i then use the checklogin, then it always returns true(it returns true after i used the logout function), why?

Link to comment
Share on other sites

Actually, I just ran your code and everything works fine.

<?php
function Login()
{
    session_start();
$_SESSION["loggedin"] = true;
}

function Logout()
{
$_SESSION["loggedin"] = false;
    session_destroy();
}

function checklogin()
{
    if ($_SESSION["loggedin"] == true)
    {
        echo "You are logged in.";
    }
    else
    {
        echo "You are logged out.";
    }
}
Login();
checklogin();
logout();
checklogin();
?>

 

Output: You are logged in.You are logged out.

 

Results were exactly as expected. Are each of these functions being called from the same file. If not then how do you have your script set up to run these functions?

Link to comment
Share on other sites

it returns true because the Login() function sets $_SESSION["loggedin"] to true when called.

 

I know that :shy:, but when i call the logout function and then call the check login, it still returns true.

 

you did not state this at all in your OP.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.