如何在 PHP 中使用户会话过期?

发布于 2024-12-09 15:54:07 字数 244 浏览 0 评论 0原文

有些人说使用 unset($_SESSION["..."]),有些人说 session_unset(),有些人说 $_SESSION = array() code> ,有些人说 session_destroy() ,我是说“看在上帝的份上,这东西变得令人困惑,有人可以解释一下哪种是注销用户的正确/安全方法”以及什么吗?是用来做什么的?

赞赏...

Some people say use unset($_SESSION["..."]) and some say session_unset() and some say $_SESSION = array() and some say session_destroy() and I am saying "for God's sake, this stuff is getting confusing, can someone please explain me which is the correct/secure way to log the user out" and what is used for what?

Appreciated...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

十雾 2024-12-16 15:54:07
<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
    $params["path"], $params["domain"],
    $params["secure"], $params["httponly"]
    );
}

// Finally, destroy the session.
session_destroy();
?>

RTM

<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
    $params["path"], $params["domain"],
    $params["secure"], $params["httponly"]
    );
}

// Finally, destroy the session.
session_destroy();
?>

RTM

我不咬妳我踢妳 2024-12-16 15:54:07

这是实体之间的区别,

您可以删除会话中的单个变量

 unset($_SESSION['shape']);

,这将删除会话中的所有变量,但不会删除会话本身,

 session_unset();

这会破坏会话变量

 session_destroy();

Here is the difference between the entities

you can remove a single variable in the session

 unset($_SESSION['shape']);

this would remove all the variables in the session, but not the session itself

 session_unset();

this would destroy the session variables

 session_destroy();
说不完的你爱 2024-12-16 15:54:07

First of all, session_destroy() is not the same as the other methods. This one will destroy the current session data on the server, but will not unset any of the variables. It's simply the counterpart to session_start().

session_unset() is the deprecated equivalent to doing $_SESSION = array(). The latter and unset($_SESSION["..."]) are different only in the fact that the unset() route will only unset a single session variable, the one named in [...]. Never do unset($_SESSION), as that will interfere with the session mechanism itself.

Old question reference.

给妤﹃绝世温柔 2024-12-16 15:54:07

唯一说 session_unset() 的是那些停留在过时版本 PHP 上的人 - 该函数已经被弃用很长时间了。

这个问题的确切答案取决于您的代码究竟使用什么来确定某人是“登录”还是“注销”。

如果您的代码要查找单个 $_SESSION['logged_in'] = true,那么为什么要取消设置它呢?只需将其设置为false,然后“boom...”用户就会注销。

The only ones saying session_unset() are the ones stuck on obsolete versions of PHP - the function's been deprecated for a LONG time now.

The exact answer to this question depends on exactly what your code uses to determine if someone is "logged in" v.s. someone who is "logged out".

If you have a single $_SESSION['logged_in'] = true that your code looks for, then why unset it? Just set it to false and boom... user is logged out.

懒猫 2024-12-16 15:54:07

session_destroy — 销毁注册到会话的所有数据
session_unset — 释放所有会话变量

http://www.php.net/manual/en /book.session.php

我见过最常用的是按此顺序调用它们。

session_unset();
session_destroy();
$_SESSION = array();

session_destroy — Destroys all data registered to a session
session_unset — Free all session variables

http://www.php.net/manual/en/book.session.php

The most I've seen used is to call them in this order.

session_unset();
session_destroy();
$_SESSION = array();
终止放荡 2024-12-16 15:54:07

如果您使用 session_destroy() 那么浏览器中的 cookie 也会被清除(并且可能稍后会创建一个新会话)

我个人使用一个对象来跟踪不同的事物(例如 public loggingIn = False; 和一个真正让用户登录的函数)

如果你想保留 coockie,session_unset() 很方便,但你最终会在服务器中得到更多的空会话

if you use session_destroy() then the cookie in the browser is also cleard (and probbley a new session gets created later)

personaly i use an object(s) to track different things (like public loggedIn = False; and a function witch actally logs the user in)

session_unset() is handy if you want to keep the coockie, but you will end up with more empty sessions in the server

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文