简体   繁体   English

PHP如何设置通过ldap获得的变量值?

[英]PHP how to set variable value got through ldap?

I'm checking whether the user is admin or not from ldap also i'm getting value from ldap 我正在检查用户是否是ldap的管理员,也正在从ldap获取价值

Part of code is, 代码的一部分是

foreach ($memberOf as $key => $value) {
    $membership = explode(",", $value);
    $member     = explode("=", $membership[0]);
    if (in_array($member[1], $adminGroups)) {
        $is_admin = TRUE;

    } elseif (in_array($member[1], $userGroups)) {
        $is_admin = FALSE;
    }

So my question, how i can use this $is_admin variable everywhere in my application so that I can hide some buttons depending on whether user is an admin or not etc? 所以我的问题是,如何在应用程序中的任何地方使用此$is_admin变量,以便根据用户是否为管理员等隐藏一些按钮?

Do I need to set this variable in session? 我是否需要在会话中设置此变量?

Use PHP's sessions! 使用PHP的会话!

Simply session_start() at the top of the files you wish to use sessions in... 只需在要在其中使用会话的文件顶部的session_start()即可。

assign a session key 分配会话密钥

$_SESSION['is_admin'] = true;

then check the key later in different parts of your application... 然后稍后在应用程序的不同部分中检查密钥...

if($_SESSION['is_admin'] === true) { /* do stuff */ }

Couple that with a function or class, you can have yourself a nice and easy authentication library. 将其与函数或类结合使用,您可以拥有一个不错且简单的身份验证库。

http://us2.php.net/manual/en/book.session.php http://us2.php.net/manual/zh/book.session.php

Edit : 编辑

I've had to use LDAP authentication a bit.. the last thing you want to do is hammer the LDAP server and get your account locked, this is why I suggested sessions. 我不得不使用LDAP身份验证..您要做的最后一件事是锤击LDAP服务器并锁定您的帐户,这就是为什么我建议使用会话的原因。 Authenticate once... once authenticated, just use the session from there on out until it expires... then the user simply logs in again. 进行一次身份验证...一旦身份验证,就可以从此开始使用会话,直到会话过期...然后用户只需再次登录即可。

There are a couple of options: 有两种选择:

  • Using an session if you want it to persist after a page reload/navigation 如果您希望会话在页面重新加载/导航后继续存在,请使用该会话
  • Use a static member in a static class so you can reference it everywhere if do not want persistence 在静态类中使用静态成员,这样就可以在不需要持久性的任何地方引用它

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

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