I am relatively new to PHP, so I realize this is very likely a beginners mistake; but I have done my due diligence and I have attempted to trouble-shoot the issue on my own, but with no luck.
First, I pass the values myusername and mypassword from the form to checklogin.php. From there it queries the database, and if a single row is returned where the username and password match, this code is run:
$_SESSION['myusername']=$myusername;
$_SESSION['mypassword']=$mypassword;
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
Now, I understand writing your own login mechanism is frowned upon --especially since mine doesn't even work. I understand this; but at this point, getting it to work is more of a learning experience for myself than a practical application.
From here I am directed to this page (login_success.php), which should only load if $_SESSION['myusername'] is set or rather, I am "logged in".
<?php
session_start();
var_dump($_SESSION);
if(!isset($_SESSION['myusername'])){
header("location:login.php");
}
var_dump($_SESSION['myusername']);
var_dump($_SESSION['mypassword']);
?>
<html>
<body>
Login Successful
</body>
</html>
Now, immediately after logging in, both var_dumps
output NULL
. If I pull the whole $_SESSION
array I get array(0) { }
.
Now, I also have a logout.php function:
<?php
session_start();
unset($_SESSION['myusername']);
$_SESSION = array();
session_destroy();
header("location:dashboard.php");
?>
a var_dump
of $_SESSION['myusername']
still shows NULL
, and a var_dump
of $_SESSION
is array(0) { }. Which is expected; HOWEVER when I got to
login_success.php, which should only load if
$_SESSION['myusername']is set, it will still load; but the
var_dumpsof the
$_SESSIONarray still show
NULL`.
So, two issues. After setting my session tokens they are always null; and after unsetting
/session_destory()
ing my $_SESSION, I can still access a page that checks to see if $_SESSION['myusername']
is set.
Does anyone know what could cause this behavior?