开发者

Why does this variable always return true?

开发者 https://www.devze.com 2023-01-31 13:33 出处:网络
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 t

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;开发者_JS百科}
}
//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)


NOT THIS if($_SESSION['logged'] = 1) THIS is correct -> if($_SESSION['logged'] == 1)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号