销毁会话,但保留一个变量集 [英] Destroy session, but keep one variable set

查看:70
本文介绍了销毁会话,但保留一个变量集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用会话变量来控制登录和页面访问.我使用变量来控制用户所属的不同用户组,所以我有相当多的会话变量.

I am using session variables to control logins and page access. I use variables to control different user groups that a user belongs to, so I have quite a few session variables.

我还使用会话变量在刷新时记住用户上次访问的页面.

I also use a session variable to remember the users last visited page upon refresh.

当用户退出时,我使用 session_destroy();删除所有变量.我想要做的是即使在用户注销后也保持最后访问的页面变量.

When the user logs out, I use session_destroy(); to remove all variables. What i would like to do is to maintain the last visited page variable even after the user has logged out.

我想我可以通过对所有其他变量使用 unset 函数来做到这一点,但有很多,并且想知道是否有更简单的方法?

I think I could do it by using the unset function on every other variable, but there are a lot, and was wondering if there was an easier way?

谢谢编辑

推荐答案

你可以试试下面的代码,

you can try below code for this,

$Arr_not_destoy_session = array('last_visited_id');

        foreach($_SESSION as $sees_key => $sess_val ){
            if(!in_array($sees_key, $Arr_not_destoy_session)){
                unset($_SESSION[$sees_key]);    
            }   
        }

这将仅取消设置除last_visited_id"之外的所有会话变量.您还可以在此数组中添加更多以后不想删除的值..

this will unset all the session variables except 'last_visited_id' only.you can also add more values in this array which you dont want to remove later on..

谢谢.

这篇关于销毁会话,但保留一个变量集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆