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. :)
it's because you're setting $_SESSION['logged'] with the single equals. Use a double equals instead:
function isLoggedIn()
{
if($_SESSION['logged'] == 1)
return true;
return false;
}
function isLoggedIn() { if($_SESSION['logged'] = 1)//assigned value '1' to $_SESSION['logged'] so in this if condition it is checking if there is any value is $_SESSION['logged'], which will be true for all the cases (valid/invalid) return true; return false; }
$loggedIn = isLoggedIn();/those the value for $loggedIn will be always true
change the if($_SESSION['logged'] = 1) to if($_SESSION['logged'] == 1)
不是这个 ($ _ SESSION ['logged'] = 1)这是正确的 - > if($ _ SESSION ['logged'] == 1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.