doubledee Posted January 7, 2012 Share Posted January 7, 2012 Isn't this code redundant... if (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] == TRUE){ After all, if $SESSION['logged_in'] == TRUE then it is *obviously* set... Debbie Quote Link to comment Share on other sites More sharing options...
Drummin Posted January 7, 2012 Share Posted January 7, 2012 Yes but as you know adding isset() will stop undefined index errors and the second check could be any number of things related to the session like $_SESSION['loggedIn'] == "Banned". But you are right, if it's TRUE then it's SET. Quote Link to comment Share on other sites More sharing options...
doubledee Posted January 7, 2012 Author Share Posted January 7, 2012 Yes but as you know adding isset() will stop undefined index errors and the second check could be any number of things related to the session like $_SESSION['loggedIn'] == "Banned". But you are right, if it's TRUE then it's SET. So it sounds like I should usually have an isset() for the undefined index issue, right? Debbie Quote Link to comment Share on other sites More sharing options...
kicken Posted January 7, 2012 Share Posted January 7, 2012 So it sounds like I should usually have an isset() for the undefined index issue, right? Anytime you can't guarantee that a variable/index exists (usually with any of the $_* vars that comes in from the request) you should use isset() to prevent the notice error. Sometimes, just checking whether it is set is enough. If you set $_SESSION['loggedin'] when the user does their login, then unset it or destroy the session data on logout, then the mere fact that $_SESSION['loggedin'] exists is enough: if (isset($_SESSION['loggedin'])){ ... } Quote Link to comment Share on other sites More sharing options...
laffin Posted January 7, 2012 Share Posted January 7, 2012 Yes, it's a good idea to use isset on variables where you are uncertain if it will be available. it's also good for setting default values if(isset($_GET['page']) $page=$_GET['page']; else $page=1; as a simple example Quote Link to comment Share on other sites More sharing options...
SergeiSS Posted January 7, 2012 Share Posted January 7, 2012 This code if(isset($_GET['page']) $page=$_GET['page']; else $page=1; is better to write in another way $page= isset( $_GET['page'] ) ? $_GET['page'] : 1; Quote Link to comment Share on other sites More sharing options...
doubledee Posted January 7, 2012 Author Share Posted January 7, 2012 So it sounds like I should usually have an isset() for the undefined index issue, right? Anytime you can't guarantee that a variable/index exists (usually with any of the $_* vars that comes in from the request) you should use isset() to prevent the notice error. Sometimes, just checking whether it is set is enough. If you set $_SESSION['loggedin'] when the user does their login, then unset it or destroy the session data on logout, then the mere fact that $_SESSION['loggedin'] exists is enough: if (isset($_SESSION['loggedin'])){ ... } Good points. Thanks, Debbie Quote Link to comment 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.