简体   繁体   English

为 PHP 中的两个不同文件夹在同一域上设置两个不同会话

[英]Setting two different sessions on same domain for two different folders in PHP

I have an application that has user management using sessions.Under the root folder of the application I have an admin panel which I need to have different session as otherwise it conflicts with the root session.我有一个使用会话进行用户管理的应用程序。在应用程序的根文件夹下,我有一个管理面板,我需要有不同的 session,否则它会与根 session 冲突。

-root
   -admin/adminFiles
   -rootFiles

I went through this thread and also the docs and tried the below code in my admin folder我浏览了这个线程和文档,并在我的管理文件夹中尝试了以下代码

if(HTTP_SERVER != 'http://localhost'){
    session_save_path("/tmp");
}
 session_name('session_for_admin');
 ini_set('session.cookie_path','/session_for_admin');
 session_set_cookie_params(60*60*24*5,'session_for_admin');
 session_start();

This just does not start the session.No error in the logs too.这只是不会启动 session。日志中也没有错误。 What am I doing wrong here.我在这里做错了什么。

I want to do it this way because the admin folder is just going to be accessed by a few privileged users and not very frequently.我想这样做是因为 admin 文件夹只会被少数特权用户访问,而且访问频率不高。 I am aware that session_name() adds and overhead.But would like to get through it this way.我知道session_name()增加了开销。但是我想通过这种方式来解决它。

so in regard to what you say about having an admin flag in your database:所以关于你所说的在你的数据库中有一个管理标志:

let's say that $admin contains a boolean: true if your database confirmed the user as an admin, false if it's not假设$admin包含一个 boolean:如果您的数据库确认用户是管理员,则为 true,否则为 false

this will activate the session with name based on the result of this boolean:这将根据此 boolean 的结果激活名称为 session 的 boolean:

if ($admin)
 {session_name('session_for_admin');}
else
 {session_name('session_for_others');}
session_start();

this will start and manage two different sessions, in the same way that two different users will have their own session.这将启动和管理两个不同的会话,就像两个不同的用户将拥有自己的 session 一样。

admittedly from there you may want to do other things such as changing the working directory, or include different files.诚然,您可能想从那里做其他事情,例如更改工作目录或包含不同的文件。

Which you could also do by just setting a variable in your session when the user is logged in as admin:当用户以管理员身份登录时,您也可以通过在 session 中设置一个变量来实现:

$_SESSION['admin'] = true;

from then on, you can check and use some files or other like this:从那时起,您可以检查和使用一些文件或其他类似的文件:

if ($_SESSION['admin'])
 {// use files in admin folder
  }
else
 {// use files in root folder
  }

Try using session_name before you start the session For example, in the first domain, use在启动 session 之前尝试使用 session_name 例如,在第一个域中,使用

session_name("AdminPanel");
session_start();

In the second domain, use在第二个域中,使用

session_name("WebsiteID");
session_start();

For more visit session name欲了解更多请访问session 姓名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM