Artsybetty Posted March 27, 2013 Share Posted March 27, 2013 (edited) 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 March 27, 2013 by Artsybetty Quote Link to comment https://forums.phpfreaks.com/topic/276233-if-_sessionuid-0-vs-if-_sessionuid-0/ Share on other sites More sharing options...
exeTrix Posted March 27, 2013 Share Posted March 27, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/276233-if-_sessionuid-0-vs-if-_sessionuid-0/#findComment-1421479 Share on other sites More sharing options...
PaulRyan Posted March 27, 2013 Share Posted March 27, 2013 You should also use isset when dealing with session variables, as they may not exist and throw an error. Quote Link to comment https://forums.phpfreaks.com/topic/276233-if-_sessionuid-0-vs-if-_sessionuid-0/#findComment-1421480 Share on other sites More sharing options...
Artsybetty Posted March 27, 2013 Author Share Posted March 27, 2013 (edited) 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 March 27, 2013 by Artsybetty Quote Link to comment https://forums.phpfreaks.com/topic/276233-if-_sessionuid-0-vs-if-_sessionuid-0/#findComment-1421481 Share on other sites More sharing options...
Solution PaulRyan Posted March 27, 2013 Solution Share Posted March 27, 2013 (edited) <?PHP if(!isset($_SESSION['uid']) || (int)$_SESSION['uid'] === 0) { //### Not Logged In echo '1'; } else { //### Logged In echo '2'; } ?> Edited March 27, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/276233-if-_sessionuid-0-vs-if-_sessionuid-0/#findComment-1421482 Share on other sites More sharing options...
Artsybetty Posted March 27, 2013 Author Share Posted March 27, 2013 excuse my VERY short hand in the previous post, ^^ thats exactly what i was looking for. thankyou both for the input! Quote Link to comment https://forums.phpfreaks.com/topic/276233-if-_sessionuid-0-vs-if-_sessionuid-0/#findComment-1421483 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.