PHP 中的 LDAP 获取当前用户详细信息


LDAP in PHP get current user details

我可以使用 AD 和 PHP 进行身份验证,我的问题是我不知道如何获取当前用户的显示名称或 cn 详细信息。这个来自 php.com 的示例代码非常适合身份验证,但现在我需要详细信息有人帮助吗?

// using ldap bind
$ldaprdn  = 'uname';     // ldap rdn or dn
$ldappass = 'password';  // associated password
// connect to ldap server
$ldapconn = ldap_connect("ldap.example.com")
    or die("Could not connect to LDAP server.");
if ($ldapconn) {
    // binding to ldap server
    $ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
    // verify binding
    if ($ldapbind) {
        echo "LDAP bind successful...";
    } else {
        echo "LDAP bind failed...";
    }
}

将搜索请求传输到服务器并解释响应。搜索请求(由 ldap_search 封装在 PHP 中)至少包含以下内容:

    基本
  • 对象(或某些文档中的基本 DN):搜索结果中不考虑优于基本 DN AIL 的对象
  • 搜索范围:onesubtreebase:搜索的深度
  • 筛选器:用于筛选(缩小)结果池
  • 请求的属性:这是 LDAP 客户端应放置cndisplayName 和处理所需的任何其他属性的位置:

参见

  • LDAP:掌握搜索过滤器
  • LDAP:搜索最佳做法
  • LDAP:编程实践

在进行身份验证之前,请将要进行身份验证的值存储到会话变量中。如果身份验证成功,请使用要显示的会话值。

如果身份验证失败,请清除会话值并尝试再次提交登录表单。

以下是在 ZF2 中将值分配给会话变量的示例代码:

  $session = new Container('base');                     
  $this->session->offsetSet('username', $username);

并获取会话值:

  $sesUserName = $this->session->offsetGet('username'); 

我希望这有所帮助。