0
votes

Trying to access $_SESSION['key'] gives me index error.

I need to check if $_SESSION['loggedin'] is set to true in order to display a login/logout button.

//GENERAL IDEA 
if( $_SESSION['loggedin'] == true ){
    //log out 
}else{
    //log in
}

But its ALWAYS executing the else and displaying the login button.

After a while, I tried to echo the $_SESSIONS['loggedin'] value and I get an index error. Checking isset() on the $_SESSION returns false.

So I think the problem is that my $_SESSION['loggedin'] isnt working like I think.

I do have:

  • session_start() at the top of my file.

  • .php extensions on all my files.


login.php creates $_SESSION['loggedin'] if successfully login in. (I know I am successfully loggedin bc I am getting redirected.)

if( $_POST['username'] == "myUSER" && $_POST['password'] == "myPASSWORD" ){
    $_SESSION['loggedin'] == true;
    header( "location: ../main/home.php" );
    exit;
}else {
    $bad_login == true;
}

Authentication.php loggedin() class function checks if $_SESSION['loggedin'] isset and true to return true

public static function loggedin(){
    return ( isset( $_SESSION['loggedin'] ) && $_SESSION['loggedin'] == true );
}

_template.php contains the button display in the footer section. ( I copied my whole template file in here bc I want to point out that my start_session() is located at the top of my home.php file BUT I don't think that makes a difference. Or maybe its easier to notice my fault if the whole file is copied. )

<?php
$page_name = basename( $_SERVER['PHP_SELF']);
$page_name_parts = explode( '.', $page_name );
?>

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Title</title>
<meta name="viewport" content="width=device-width">
</head>

<body>
    <div class="container-fluid">

        <header>
            <h1 id="top">Heading</h1>
        </header>

        <?php
            //Flash::show_flash();
            require( $page_name_parts[0] . ".view.php" );
        ?>

        <footer>
            <!-- Login button -->
            <?php require_once( "../lib/Authentication.php" ); ?>
            <?php if( Authentication::loggedin() ): ?>
                <a href=" ../authentication/logout.php"><button class="btn1">Logout</button></a>
            <?php else : ?>
                <a href=" ../authentication/login.php"><button class="btn1">Login</button></a>
            <?php endif ?>

            <!-- Scroll TOP -->
            <a href="#top"><span></span>Top</a>
        </footer>
    </div>
</body>

</html>
3
It happens when $_SESSION['loggedin'] is not set. What you should do is check if index isset if(isset($_SESSION['loggedin']) && $_SESSION['loggedin']) { //you are logged in} else { //not logged in} hywak

3 Answers

0
votes

You don't set the $_SESSION['loggedin'], you test if it's true. Change the == to =.

-1
votes

I do have session_start() at the top of my file.

But I can't see it in any of the code you have posted.

Your methodology is also flawed.

Yes, your login page is probably setting the value and saving it, but its not being read subsequently. The first pace you should be looking is at your error reporting/logging to make sure you haven't got a headers already sent warning.

Then check your HTTP request and response headers to make sure your browser is sending back the same session cookie value used in the login page. You may have a cookie path issue.

That covers 99% of cases - but if you've copied the code correctly, then your problem is here:

if( $_POST['username'] == "myUSER" && $_POST['password'] == "myPASSWORD" ){
    $_SESSION['loggedin'] == true;

You are not setting the value at login - '==' is a comparison not an assignment.

-1
votes