I recently looked at the session issue and needed to determine whether the user is logged on. I found that either of the following methods can be used to determine whether the user is logged on. The code is as follows: {code ...} problem: In the second judgment statement, if you only write $ _ SESSION instead of $ _ SESSION [& #039; uname & #039;], an error is reported... I recently looked at the session issue and needed to determine whether the user is logged on. I found that either of the following methods can be used to determine whether the user is logged on.
The code is as follows:
First: if (empty ($ _ SESSION) {echo 'you have not logged on. please log on';} Else {echo 'Welcome'. $ _ SESSION ['uname']. 'here is the homepage'; echo 'to exit
';} Second: if (! Isset ($ _ SESSION ['uname']) {echo 'you have not logged on yet. please log on
';} Else {echo 'Welcome'. $ _ SESSION ['uname']. 'here is the homepage'; echo 'to exit
';}
Problem: In the second judgment statement, if you only write $ _ SESSION instead of $ _ SESSION ['uname'], an error is returned. if you use the first method, there is no problem in the brackets behind empty, whether it is $ _ SESSION ['uname'] or $ _ SESSION. why? Which of the two methods is better? Or is it safer? Thank you.
Reply content:
I recently looked at the session issue and needed to determine whether the user is logged on. I found that either of the following methods can be used to determine whether the user is logged on.
The code is as follows:
First: if (empty ($ _ SESSION) {echo 'you have not logged on. please log on';} Else {echo 'Welcome'. $ _ SESSION ['uname']. 'here is the homepage'; echo 'to exit
';} Second: if (! Isset ($ _ SESSION ['uname']) {echo 'you have not logged on yet. please log on
';} Else {echo 'Welcome'. $ _ SESSION ['uname']. 'here is the homepage'; echo 'to exit
';}
Problem: In the second judgment statement, if you only write $ _ SESSION instead of $ _ SESSION ['uname'], an error is returned. if you use the first method, there is no problem in the brackets behind empty, whether it is $ _ SESSION ['uname'] or $ _ SESSION. why? Which of the two methods is better? Or is it safer? Thank you.
The second type is recommended. With the correct use of the second method, the code will be more rigorous.
Whether empty is empty or whether isset is a variable.
As long as you enable the session function, session_start (); then an array variable $ _ SESSION exists.
However, if you do not log on, $ _ SESSION is just an empty array. only when you log on will you write data to $ _ SESSION, such
$ _ SESSION ['uname'].
Therefore, the second reason for writing only $ _ SESSION is that you have not logged in, so $ _ SESSION ['uname'] does not exist, however, $ _ SESSION exists as an empty array.
Empty determines whether the parameter is considered null. Even if the parameter exists but its value is false, the parameter is still considered null.
If (isset ($ _ SESSION ['uname']) &! Empty ($ _ SESSION ['uname']) {// That's all right
}
The difference between isset and empty: the processing objects of empty () and isset () are almost undefined variables, 0, empty strings.
If the variable is 0, empty () returns TRUE, and isset () returns TRUE. if the variable is a null string, empty () returns TRUE, isset () TRUE is returned. if the variable is not defined, empty () returns TRUE, and isset () returns FLASE;