Jerms01 Posted December 5, 2014 Share Posted December 5, 2014 Hey Guys, Been coding PHP for a while but I always wonder about the right way of doing things. I am building an online community which I want to display to members and non-members. With this each screen will have options that are available for members only. I have set up session variables once a user logged in but its getting really old having to nest if statements on ISSET then again to check the values in the variables if it is set. See example below. Question 1. Is it ok to session_start(); for all site visitors? Question 2. If Q.1 is ok then is it ok to set all the session variables upfront with blank values as placeholders. This would eliminate the need for ISSET. if (ISSET($_SESSION['On'])) { if (in_array($GroupID, $_SESSION['Groups'])) { $IsMember = 1; } else {$IsMember = 0;} } else {$IsMember = 0;} Just wanted to get your thoughts on this. Thank you, Jeremy Quote Link to comment Share on other sites More sharing options...
Solution jcbones Posted December 5, 2014 Solution Share Posted December 5, 2014 Q1. Yes. Q2. You can build a function to test it. function sessionVar($key) { return isset($_SESSION[$key]) ? $_SESSION[$key] : null; } echo 'Now using an empty session ' . sessionVar('index') . ' will not throw an error.'; Although, in this case the function would need to be more robust, as it would throw an error on an empty session variable being sent to is_array(). Quote Link to comment Share on other sites More sharing options...
Jerms01 Posted December 5, 2014 Author Share Posted December 5, 2014 Awesome thank you! This will save tons of time. 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.