如何在注销时取消设置特定的 php 会话

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

How to unset a specific php session on logout

phpsessionlogout

提问by Joan Silverstone

I have 2 sites.

我有2个网站。

In one site this is true:

在一个站点中,这是真的:

session_is_registered('site1sess')

and in the other one this is true:

在另一个中,这是真的:

session_is_registered('site2sess')

Those are the session names I give users on login. My problem is that when I logout from one site, I also logout in the other one because I use:

这些是我在登录时给用户的会话名称。我的问题是,当我从一个站点注销时,我也在另一个站点注销,因为我使用:

session_destroy(); 

What is the best way to logout from site1 or 2 deleting all the session variables from it? Thank you.

从 site1 或 2 注销并从中删除所有会话变量的最佳方法是什么?谢谢你。

回答by Abs

Use unset()for all the session variables specific to either site 1 or 2.

使用unset()的所有会话变量具体到站点1或2。

unset($_SESSION['site1']);
//or
unset($_SESSION['site2']);

Just so that you know, session_is_registeredis deprecated as of PHP version 5.3.0. See docs.

只是为了让您知道,session_is_registered自 PHP 5.3.0 版起已弃用。请参阅文档

回答by user92635438

Before unset($_SESSION['site1']);put session_start()like this

像这样unset($_SESSION['site1']);放之前session_start()

<?php
    session_start();
    unset($_SESSION['site1']);
?>

回答by FinalForm

When you log out of 1

当您退出 1

unset($_SESSION['site1sess']);

Or when you log out of the other

或者当你退出另一个

unset($_SESSION['site2sess']);

回答by Ashish v

You can unset session while you don't want to logout logged in user.

当您不想注销登录用户时,您可以取消设置会话。

if(isset($_GET['logout'])) {
   session_unset($_SESSION['user']);
}