如何检查PHP会话是否为空?

时间:2022-05-25 07:18:14

Is this bad practice?

这是不好的做法吗?

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';
}

Does !isset just checks if a $_SESSION[''] exist and doesn't check if, is there are values in the array or not

是否!isset只检查$ _SESSION ['']是否存在,并且不检查数组中是否有值

7 个解决方案

#1


77  

I would use isset and empty:

我会使用isset和empty:

session_start();
if(isset($_SESSION['blah']) && !empty($_SESSION['blah'])) {
   echo 'Set and not empty, and no undefined index error!';
}

array_key_exists is a nice alternative to using isset to check for keys:

array_key_exists是使用isset检查密钥的不错的替代方法:

session_start();
if(array_key_exists('blah',$_SESSION) && !empty($_SESSION['blah'])) {
    echo 'Set and not empty, and no undefined index error!';
}

Make sure you're calling session_start before reading from or writing to the session array.

确保在读取或写入会话数组之前调用session_start。

#2


10  

Use isset, empty or array_key_exists (especially for array keys) before accessing a variable whose existence you are not sure of. So change the order in your second example:

在访问存在您不确定的变量之前,请使用isset,empty或array_key_exists(尤其是数组键)。因此,在第二个示例中更改顺序:

if (!isset($_SESSION['something']) || $_SESSION['something'] == '')

#3


6  

you are looking for PHP’s empty() function

你正在寻找PHP的empty()函数

#4


5  

You could use the count() function to see how many entries there are in the $_SESSION array. This is not good practice. You should instead set the id of the user (or something similar) to check wheter the session was initialised or not.

您可以使用count()函数查看$ _SESSION数组中有多少条目。这不是好习惯。您应该设置用户的ID(或类似的东西)以检查会话是否已初始化。

if( !isset($_SESSION['uid']) )
    die( "Login required." );

(Assuming you want to check if someone is logged in)

(假设您要检查是否有人登录)

#5


4  

If you want to check whether sessions are available, you probably want to use the session_id() function:

如果要检查会话是否可用,可能需要使用session_id()函数:

session_id() returns the session id for the current session or the empty string ("") if there is no current session (no current session id exists).

#6


0  

if(isset($_SESSION))
{}
else
{}

#7


-1  

This is not a bad practice. But you might get an error like

这不是一个坏习惯。但是你可能会收到类似的错误

undefined index(the key you defined )

未定义的索引(您定义的键)

So the best practice is to check if the array key exists using the built-in array_key_exists function.

因此,最佳做法是使用内置的array_key_exists函数检查数组键是否存在。

#1


77  

I would use isset and empty:

我会使用isset和empty:

session_start();
if(isset($_SESSION['blah']) && !empty($_SESSION['blah'])) {
   echo 'Set and not empty, and no undefined index error!';
}

array_key_exists is a nice alternative to using isset to check for keys:

array_key_exists是使用isset检查密钥的不错的替代方法:

session_start();
if(array_key_exists('blah',$_SESSION) && !empty($_SESSION['blah'])) {
    echo 'Set and not empty, and no undefined index error!';
}

Make sure you're calling session_start before reading from or writing to the session array.

确保在读取或写入会话数组之前调用session_start。

#2


10  

Use isset, empty or array_key_exists (especially for array keys) before accessing a variable whose existence you are not sure of. So change the order in your second example:

在访问存在您不确定的变量之前,请使用isset,empty或array_key_exists(尤其是数组键)。因此,在第二个示例中更改顺序:

if (!isset($_SESSION['something']) || $_SESSION['something'] == '')

#3


6  

you are looking for PHP’s empty() function

你正在寻找PHP的empty()函数

#4


5  

You could use the count() function to see how many entries there are in the $_SESSION array. This is not good practice. You should instead set the id of the user (or something similar) to check wheter the session was initialised or not.

您可以使用count()函数查看$ _SESSION数组中有多少条目。这不是好习惯。您应该设置用户的ID(或类似的东西)以检查会话是否已初始化。

if( !isset($_SESSION['uid']) )
    die( "Login required." );

(Assuming you want to check if someone is logged in)

(假设您要检查是否有人登录)

#5


4  

If you want to check whether sessions are available, you probably want to use the session_id() function:

如果要检查会话是否可用,可能需要使用session_id()函数:

session_id() returns the session id for the current session or the empty string ("") if there is no current session (no current session id exists).

#6


0  

if(isset($_SESSION))
{}
else
{}

#7


-1  

This is not a bad practice. But you might get an error like

这不是一个坏习惯。但是你可能会收到类似的错误

undefined index(the key you defined )

未定义的索引(您定义的键)

So the best practice is to check if the array key exists using the built-in array_key_exists function.

因此,最佳做法是使用内置的array_key_exists函数检查数组键是否存在。