Jump to content

Session Handling


Go to solution Solved by jcbones,

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/292916-session-handling/
Share on other sites

  • Solution

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().

Link to comment
https://forums.phpfreaks.com/topic/292916-session-handling/#findComment-1498653
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.