I am trying to write a login script. After logging in a session variable is set, and on the main page, the isLoggedIn function is run. The problem is, that the variable $loggedIn is always returning true. Can someone help?
function validateUser($user)
{
if(isset($user)){
session_regenerate_id ();//this is a security measure
$_SESSION['logged'] = 1;
$_SESSION['userid'] = $user;}
}
//Validates Login
function isLoggedIn()
{
if($_SESSION['logged'] = 1)
return true;
return false;
}
$loggedIn = isLoggedIn();
if($loggedIn){ SHOW CONTENT FOR LOGGED IN USERS }
else { show content for users not logged in }
You are using the assignment operator = instead of a conditional operator ==.
It's supposed to be:
if ($_SESSION['logged'] == 1)
return true;
The result of an assignment = is the right hand side expression, which is 1, which is always true in this case. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With