如何检查 PHP 会话是否存在?

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

How can you check if a PHP session exists?

phpsession

提问by ARW

Just wondering how to check if a PHP session exists... My understanding is that no matter what, if I am using sessions, I have to start my files with session_start() to even access the session, even if I know it already exists.

只是想知道如何检查 PHP 会话是否存在...我的理解是,无论如何,如果我使用会话,我必须使用 session_start() 启动我的文件才能访问会话,即使我知道它已经存在.

I've read to user session_id() to find out if a session exists, but since I have to use session_start() before calling session_id(), and session_start() will create a new ID if there isn't a session, how can I possible check if a session exists?

我已向用户 session_id() 阅读以了解会话是否存在,但由于我必须在调用 session_id() 之前使用 session_start(),如果没有会话,session_start() 将创建一个新 ID,如何我可以检查会话是否存在吗?

回答by xenak

In PHP versions prior to 5.4, you can just the session_id()function:

在 5.4 之前的 PHP 版本中,您可以只使用以下session_id()函数:

$has_session = session_id() !== '';

In PHP version 5.4+, you can use session_status():

在 PHP 5.4+ 版本中,您可以使用session_status()

$has_session = session_status() == PHP_SESSION_ACTIVE;

回答by Simon Carlson

isset($_SESSION)

That should be it. If you wanna check if a single session variable exists, use if(isset($_SESSION['variablename'])).

应该是这样。如果要检查单个会话变量是否存在,请使用if(isset($_SESSION['variablename'])).

回答by hakre

In PHP there is something called the session name. The name is co-related to the cookie that will be being set ifthe session was already started.

在 PHP 中有一个叫做会话名称的东西。如果会话已经启动,则该名称与将要设置的 cookie 相关。

So you can check the $_COOKIEarray if there is a session cookie available. Cookies are normally the preferred form to interchange the session id for the session name with the browser.

因此,您可以检查$_COOKIE数组是否有可用的会话 cookie。Cookie 通常是与浏览器交换会话名称的会话 ID 的首选形式。

If a cookie already exists this means that a PHP session was started earlier. If not, then session_start()will create a new session id and session.

如果 cookie 已经存在,这意味着 PHP 会话已提前启动。如果没有,session_start()则将创建一个新的会话 ID 和会话。

A second way to check for that is to check the outgoing headersif the cookie for the session is set there. It will be set if it's a new session. Or if the session id changed.

第二种检查方法是检查传出的标头,如果会话的 cookie 设置在那里。如果它是一个新会话,它将被设置。或者,如果会话 ID 发生了变化。

回答by Steve Piper

I find it best many times (depends on the nature of the application) to simply test to see if a session cookie is set in the client:

我发现最好多次(取决于应用程序的性质)简单地测试以查看是否在客户端中设置了会话 cookie:

<?php

if (isset($_COOKIE["PHPSESSID"])) {
    echo "active";
} else {
    echo "don't see one";
}

?>

Of course, replace the default session name "PHPSESSID" with any custom one you are using.

当然,用您正在使用的任何自定义会话名称替换默认会话名称“PHPSESSID”。

回答by Roman

isset($_SESSION)isn't sufficient because if a session has been created and destroyed (with session_destroy()) in the same execution, isset($_SESSION)will return true. And this situation may happen without your knowing about it when a 3rd party code is used. session_id()correctly returns an empty string, though, and can be called prior to session_start().

isset($_SESSION)是不够的,因为如果session_destroy()在同一次执行中创建和销毁会话(使用),isset($_SESSION)将返回true。当使用第 3 方代码时,这种情况可能会在您不知情的情况下发生。session_id()但是,正确返回一个空字符串,并且可以在session_start().

回答by Trilarion

Store the session_idin $_SESSIONand check against it.

存放session_id$_SESSION和对证。

First time

第一次

session_start();
$_SESSION['id'] = session_id();

Starts a session and stores the randomly given session id.

启动一个会话并存储随机给定的会话 ID。

Next time

下次

session_start();
$valid_session = isset($_SESSION['id']) ? $_SESSION['id'] === session_id() : FALSE;
if (!$valid_session) {
   header('Location: login.php');
   exit();
}

Starts a session, checks if the current session id and the stored session id are identical (with the ternary ? as replacement for the non-existing short circuit AND in php). If not, asks for login again.

启动一个会话,检查当前会话 id 和存储的会话 id 是否相同(用三元 ? 作为 php 中不存在的短路 AND 的替代)。如果没有,则要求再次登录。

回答by Harshit Chaturvedi

switch off the error reporting if noting is working in your php version put top on your php code

如果注意在您的 php 版本中正常工作,请关闭错误报告将顶部放在您的 php 代码上

error_reporting(0);

error_reporting(0);

回答by Dave

Check if session exists before calling session_start()

在调用 session_start() 之前检查 session 是否存在

if(!isset($_SESSION))session_start();

回答by craig1231

You can call session_id before session_start. http://www.php.net/manual/en/function.session-id.php- read the id param

您可以在 session_start 之前调用 session_id。http://www.php.net/manual/en/function.session-id.php- 读取 id 参数

回答by SpYk3HH

I've always simply used

我一直只是简单地使用

if (@session_id() == "") @session_start();

Hasn't failed me yet.

还没有让我失望。

Been quite a long time using this.

使用这个已经很长时间了。

NOTE: @simply suppresses warnings.

注意@只是抑制警告。