Changing Servers
18 05 2009ILuvJohn.Com will be changing servers very soon. Just giving you a heads up.
Thanks!
Written by: John Minton
Comments : 1 Comment »Categories : Welcome To ILuvJohn
ILuvJohn.Com will be changing servers very soon. Just giving you a heads up.
Thanks!
Written by: John Minton
Comments : 1 Comment »One of my first websites used sessions to validate user login. I used a very basic include system and for the most part it seamed to work really well.
Then one day the login stopped working correctly. A friend mentioned to me that the SESSION might be buggy when validating that the user is logged in or not. He explained that if I was using
<?php
if(!empty($_SESSION['userid'])){
// logged in page here
}else{
//not logged in page here
}
?>
that I should try using
<?php
if(isset($_SESSION['userid'])){
// logged in page here
}else{
//not logged in page here
}
?>
and vice-versa.
For years I have not figured out exactly what that was. But now after searching for an answer I found an interesting issue in the PHP forums: The different between the PHP functions empty() and isset().
At first I thought it was only a few people and that someone had set them straight. But I was wrong! Hundreds, it seamed, of people who where asking the same question over and over and over again were getting the wrong answer just as much. Not even one person had posted an answer that was close to where my mind was.
Here is the low-down on these two functions, isset() and empty().
Here is an example of isset()’s intended usage:
<?php
$var = "asdf"; // set var, equales a string
$var = ""; // empty out the variable
if(isset($var)){echo "var isset";}else{echo "var is not set";}
echo "<br /><br />";
$var2 = ""; // set an empty variable
if(isset($var)){echo "var2 isset";}else{echo "var2 is not set";}
echo "<br /><br />";
$unsetvar = "Unset me"; // set a variable with a string.
unset($unsetvar); // destroy the variable.
if(isset($unsetvar)){echo "unsetvar isset";}else{echo "unsetvar is not set";}
echo "<br /><br />";
if(isset($this_is_not_here)){echo "this_is_not_here isset";}else{echo "this_is_not_here is not set";}
?>
This will output the following:
var isset
var2 isset
unsetvar is not set
this_is_not_here is not set
You can plainly see that even the empty variables will still pass isset(), while they would not pass !empty().
To say it is simply:
When you set a variable in PHP it “opens it up”, or sets aside a space in the memory for it.
isset() determines if a variable has a place emptied out in memory for it. In other words, it wants to see if the variable has been set.
At the same time, if a variable is !empty() then it is isset(), and the two are the one in the same.
You can see how some applications can use either. In my opinion, unless needed otherwise it is probably best to stick with the empty() function. I wouldn’t be able to tell you for sure how or why, it’s more like a guy feeling I have…. But I could be totally wrong.
The difference between isset() and empty() is that isset() checks to see if a variable has been used even if it is empty, while empty() just checks the state of the contents of the variable.
You can cause a variable tat is set to be completely removed from the memory by using unset($var). If you unset() a variable and then check it with isset(), it will return false as unset() completely removes any traces of the variable.
Questions? Comments? Can’t figure something out? Email me on the contact form or leave a comment!
Written by: John Minton
Comments : No Comments »