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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1519818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:55:04  来源:igfitidea点击:

How do check if a PHP session is empty?

phpsession

提问by willdanceforfun

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 !issetjust checks if a $_SESSION['']exist and doesn't check if, is there are values in the array or not

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

回答by karim79

I would use issetand empty:

我会使用issetempty

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

array_key_existsis a nice alternative to using issetto check for keys:

array_key_existsisset用于检查密钥的一个不错的替代方法:

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_startbefore reading from or writing to the session array.

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

回答by Gumbo

Use isset, emptyor 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,emptyarray_key_exists(特别是对于数组键)。因此,更改第二个示例中的顺序:

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

回答by knittl

you are looking for PHP's empty()function

您正在寻找 PHP 的empty()功能

回答by svens

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)

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

回答by mjs

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).
session_id() 返回当前会话的会话 ID,如果没有当前会话(不存在当前会话 ID),则返回空字符串 ("")。

回答by hfarazm

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

回答by Glenn

I know this is old, but I ran into an issue where I was running a function after checking if there was a session. It would throw an error everytime I tried loading the page after logging out, still worked just logged an error page. Make sure you use exit(); if you are running into the same problem.

我知道这是旧的,但我遇到了一个问题,在检查是否有会话后我正在运行一个函数。每次我在注销后尝试加载页面时,它都会抛出一个错误,但仍然可以正常工作,只是记录了一个错误页面。确保使用 exit(); 如果你遇到同样的问题。

     function sessionexists(){
        if(!empty($_SESSION)){
     return true;
         }else{
     return false;
         }
      }


        if (!sessionexists()){
           redirect("https://www.yoursite.com/");
           exit();
           }else{call_user_func('check_the_page');
         } 

回答by Reduan Rafi

The best practice is to check if the array key exists using the built-in array_key_existsfunction.

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