Artsybetty Posted March 27, 2013 Share Posted March 27, 2013 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 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. 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. 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 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? 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...
PaulRyan Posted March 27, 2013 Share Posted March 27, 2013 <?PHP if(!isset($_SESSION['uid']) || (int)$_SESSION['uid'] === 0) { //### Not Logged In echo '1'; } else { //### Logged In echo '2'; } ?> 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! 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
Archived
This topic is now archived and is closed to further replies.