Originally posted by IceD To MrAlaska:
empty vs isset.... I think, whe should use isset instead of empty - imagine the situation, when there are no session variables set at all - both $SESSION and $HTTP_SESSION_VARS will be empty. Also, instead of using mysql functionas after setting session variables we should use custom session functions handlers. In this case we produce better and less buggy code.
!empty vs isset. When initializing variables I agree. You MUST use isset in that case.
However, when collecting variables that should already be initialized I want to explicitly assign the variables to nothing if they do not exist, for instance
if (!empty($SESSION)) {
$user_id=$SESSION['id'];
// collect all variables
}
elseif(!empty($HTTP_SESSION_VARS)) {
$user_id=$HTTP_SESSION_VARS['id'];
// collect all variables
}
else {
$user_id="" ;
// set them all to ""
}
[EDIT] in many cases it would make more sense to collect them individually, in which case we would use isset:
if (isset ($SESSION['id'])) $user_id=$SESSION['id'];
elseif(isset($HTTP_SESSION_VARS)) $user_id=$HTTP_SESSION_VARS['id'];
else $user_id="";
[/EDIT]
If we use isset instead of !empty for collecting a group and the $_SESSION variables have not been initialized, it will fall into one of the first if conditions and $user_id will BE nothing instead of being SET to nothing. The difference is subtle and the code will still function, however if you have error_reporting set to E_ALL (which you can do via your code by puting error_reporting(E_ALL) at the beginning of your page) it will throw an undeclared variable notice every time you use $user_id.
When setting them, we need to use isset:
if (isset($SESSION)) {
$SESSION['id']=$row['user_id'];
}
elseif(isset($HTTP_SESSION_VARS)) {
$HTTP_SESSION_VARS['id']=$row['user_id'];
}
The same is true with POST, GET, and all the GLOBAL members. They will be SET but EMPTY until you populate them.
And if track_vars is disabled:
$GLOBALS['HTTP_SESSION_VARS']['id']=$row['id'];
$user_id=$GLOBALS['HTTP_SESSION_VARS']['id'];
should give you access to the arrays so you do not need to use session_register(), but I haven't had time to play with it. That should work even if track_vars AND register_globals is set to 0 which would be a difficult server to work with but might have some performance benifits. I am definately gonna play with that one, thank you for bringing it to my attention. I had never considered the option of track_vars being off.
Regarding mysql functions, in the example I posted, the mysql functions were just an example of how one might validate a member against the database, if I was using native session I would probably also collect and register many of the user variables as $SESSION variables at that point. When using $SESSION you might as well use the features, since you're paying for them anyway. 😃
I do like the function you use to register variables, and the concept of making it server independant appeals to me.
[EDIT AGAIN] I am now building a function simlar to yours to collect my POST and GET variables, so I will not be using !empty in my code anymore[/EDIT AGAIN]