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>