如何检查PHP会话是否为空? [英] How do check if a PHP session is empty?
本文介绍了如何检查PHP会话是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是不好的做法吗?
if ($_SESSION['something'] == '')
{
echo 'the session is empty';
}
是否可以检查其是否为空或未设置?我实际上是在这样做:
Is there a way to check if its empty or it is not set? I'm actualy doing this:
if (($_SESSION['something'] == '') || (!isset($_SESSION['something'])) {
echo 'the session is either empty or doesn\'t exist';
}
!isset
只是检查$_SESSION['']
是否存在,并且不检查数组中是否有值
Does !isset
just checks if a $_SESSION['']
exist and doesn't check if, is there are values in the array or not
推荐答案
I would use isset
and empty
:
session_start();
if(isset($_SESSION['blah']) && !empty($_SESSION['blah'])) {
echo 'Set and not empty, and no undefined index error!';
}
array_key_exists
是使用isset
检查密钥的不错选择:
array_key_exists
is a nice alternative to using isset
to check for keys:
session_start();
if(array_key_exists('blah',$_SESSION) && !empty($_SESSION['blah'])) {
echo 'Set and not empty, and no undefined index error!';
}
在读取或写入会话数组之前,请确保要调用session_start
.
Make sure you're calling session_start
before reading from or writing to the session array.
这篇关于如何检查PHP会话是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文