Jump to content

if ($_SESSION['uid'] > 0) VS if ($_SESSION['uid'] == 0)


Artsybetty
Go to solution Solved by PaulRyan,

Recommended Posts

this will be used to check for a valid user id to show certain pages to logged in users, does it make a difference (beside the obvious order after the statements) and if so what method is more secure?

 

any suggestions are appreciated :)

Edited by Artsybetty
Link to comment
Share on other sites

They would be equivalent if done like this:

 


if( !( $var > 0 ) ){}

if( $var == 0 ){} 

However this would be better:

 

if( (int) $var === 0 ){}

 Basically with the latter you're testing type and value. However, remember that if you cast a string as a int which starts with a letter it'll always return 0.

Link to comment
Share on other sites

so, as my uid contains no letters,

 

if isset( (int) $_SESSION['uid'] === 0 ){ not logged content}

else {logged in content}

 

should work?

 

and i'm presuming that (based on your post) checking for non logged users first is the best route?

Edited by Artsybetty
Link to comment
Share on other sites

  • Solution


<?PHP
 
  if(!isset($_SESSION['uid']) || (int)$_SESSION['uid'] === 0) {
    //### Not Logged In
    echo '1';
  } else {
    //### Logged In
    echo '2';
  }
 
?>

 

Edited by PaulRyan
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.