Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this variable always return true?

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 }
like image 980
mcbeav Avatar asked Jun 12 '26 09:06

mcbeav


1 Answers

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. :)

like image 179
RabidFire Avatar answered Jun 14 '26 23:06

RabidFire