php 检查php中是否存在会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32837397/
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 23:13:55  来源:igfitidea点击:

Check if session exists in php

phpsession

提问by CJAY

In my php function here, i want to check if the session exists or not. based on the session existence i want to return true or false.

在我的 php 函数中,我想检查会话是否存在。基于会话的存在,我想返回 true 或 false。

i have a login function which uses session_start();and stores values into session variables when logged in, and when logged out it will do session_destroy();

我有一个登录函数,它在登录时使用session_start();并将值存储到会话变量中,当注销时它会做session_destroy();

now i want to check if the session exists or not. How can i do that

现在我想检查会话是否存在。我怎样才能做到这一点

function ifsessionExists(){
    // check if session exists?
    if($_SESSION[] != ''){
    return true;
    }else{
    return false;
    }
}

回答by Ajeet Kumar

Recommended way for versions of PHP >= 5.4.0

PHP >= 5.4.0 版本的推荐方式

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

Source: http://www.php.net/manual/en/function.session-status.php

For versions of PHP < 5.4.0

来源:http: //www.php.net/manual/en/function.session-status.php

对于 PHP < 5.4.0 的版本

if(session_id() == '') {
    session_start();
}

回答by aldrin27

Use issetfunction

使用isset功能

function ifsessionExists(){
// check if session exists?
  if(isset($_SESSION)){
    return true;
  }else{
    return false;
  }
 }

You can also use emptyfunction

您还可以使用empty功能

 function ifsessionExists(){
// check if session exists?
  if(!empty($_SESSION)){
    return true;
  }else{
    return false;
  }
 }

回答by Harshit Shrivastava

Use

function ifsessionExists(){
// check if session exists?
if(isset($_SESSION['Key']) && $_SESSION['Key'] == 'Value'){
return true;
}else{
return false;
}
}

回答by Ninju

You can use isset

您可以使用 isset

function ifsessionExists(){
    //check if session exists?
    if (isset($_SESSION['key'])){
    return true;
    }else{
    return false;
    }
}

回答by LIMEXS

Try this:

尝试这个:

function ifsessionExists(){
    // check if session exists?
    if(isset($_SESSION)){
    return true;
    }else{
    return false;
    }
}

Actualy, this is the better way to do this as suggested on session_status() documentation page:

实际上,这是按照 session_status() 文档页面上的建议执行此操作的更好方法:

<?php
/**
* @return bool
*/
function is_session_started()
{
    if ( php_sapi_name() !== 'cli' ) {
        if ( version_compare(phpversion(), '5.4.0', '>=') ) {
            return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
        } else {
            return session_id() === '' ? FALSE : TRUE;
        }
    }
    return FALSE;
}

// Example
if ( is_session_started() === FALSE ) session_start();
?>

Here you can read more about it http://sg2.php.net/manual/en/function.session-status.php#113468

在这里你可以阅读更多关于它http://sg2.php.net/manual/en/function.session-status.php#113468

回答by Sougata Bose

You can use session_id().

您可以使用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),则返回空字符串 ("")。

function ifsessionExists(){
    // check if session exists?
    $sid= session_id();
    return !empty($sid);
}