从同一个文件 (PHP) 在两个会话中写入 [英] Writing in two sessions from the same file (PHP)

查看:54
本文介绍了从同一个文件 (PHP) 在两个会话中写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让管理员可以从后端以 fontend 用户身份登录.现在我正在使用两个会话(具有不同名称的会话),一个用于管理员,另一个用于前端.

I want to make it possible for the administrator to log in as a fontend user from the backend. Right now I'm using two sessions (sessions with different names), one for the admin and one for the frontend.

是否可以在第一个会话中写入,关闭它然后打开一个新会话?

Is it possible to write in the first session, close it and then open a new session?

这是我尝试但失败的简化版本:

This is a simplified version of what I attempted but failed:

session_name('admin_session');
session_start();
// use first session without generatring any output
session_close();

session_name('frontend_session');
session_start();
// use the second session

谢谢.

推荐答案

我过去曾为管理员提供了切换到用户"的便利.

I have provided the facility for an administrator to switch to a "user" in the past.

我通过序列化管理员用户的 $_SESSION 然后将该字符串存储为 $_SESSION['adminData'] 来做到这一点,然后用他登录的用户的那些会话变量覆盖所有其他会话变量,并重定向到主页.主页上唯一不同的功能是针对 $_SESSION['adminData'] 的 if 测试,它提供了一个恢复到管理员"链接……但出于所有意图和目的,管理员现在以该用户身份登录.单击恢复为管理员"链接时,代码逻辑只是将 $_SESSION['adminData'] 反序列化.

I did this by serializing the admin user's $_SESSION and then storing that string as $_SESSION['adminData'] before overwriting all the other session vars with those of the user he was logging in as, and redirecting to the main page. The only different feature on the main page was an if test against $_SESSION['adminData'] that provided a "revert to admin" link... but to all intents and purposes, the administrator was now logged in as that user. On clicking the "revert to admin" link, the code logic simply unserialized $_SESSION['adminData'].

这篇关于从同一个文件 (PHP) 在两个会话中写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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