Codeigniter & PHP 检查会话是否存在

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

Codeigniter & PHP check if session exist

phpcodeignitersessioncookiescontrols

提问by itsme

How can i simply check if cookies are enabled and user session too in PHP?

我如何简单地检查是否在 PHP 中启用了 cookie 和用户会话?

I need the really lighter piece of code of this world to do that, can anyone show me somenthing?

我需要这个世界上真正更轻的代码来做到这一点,谁能给我看点什么?

I'm on Codeigniter but i'm planning to use native PHP for this control.

我在 Codeigniter 上,但我计划使用本机 PHP 进行此控件。

my code:

我的代码:

if(session_start()){ echo 'started'; }

as i know Codeigniter doesn't uses native PHP session, how to do so?

据我所知,Codeigniter 不使用本机 PHP 会话,该怎么做?

回答by Brendan Bullen

Check for a valid session id:

检查有效的会话 ID:

$sid = session_id();

For example:

例如:

$sid = session_id();
if($sid) {
    echo "Session exists!";
} else {
    session_start();
}

回答by René H?hle

The first point is "Don't fight the framework"when your framework has some functions than use it. Normally in the Framework classes are functions to prevent injections.

当您的框架具有一些功能而不是使用它时,第一点是“不要与框架作斗争”。通常在框架类中是防止注入的函数。

Here is a Post when tells you how to check the cookie:

这是一个帖子,告诉您如何检查 cookie:

Check if cookies are enabled

检查 cookie 是否已启用

And for the session

对于会议

How to tell if a session is active?

如何判断会话是否处于活动状态?

回答by Smita

I think you can simply check by doing something like:

我认为您可以通过执行以下操作简单地进行检查:

if(isset($_SESSION)){
  // tells php session is initiated
}
if(isset($_COOKIE)){

}