【问题标题】:PHP LDAP: Error encountered on ldap_searchPHP LDAP:在 ldap_search 上遇到错误
【发布时间】:2014-01-03 02:11:26
【问题描述】:

我在互联网上得到了这段代码并做了一些小修改。我计划通过将他们输入的用户 ID 和密码与我们的 AD 匹配来验证我网站的用户。

代码似乎可以工作,但是当我尝试添加邮件属性时,我收到了这个错误:

Notice: Undefined index: mail in C:\xampp\htdocs\ldap\index2.php on line 34
Retrieved 1 Active Directory users 

请帮我找出错误。我想获取用户的邮件并将其显示在屏幕上。

<?php

$ldap_password = "mypassword";
$ldap_username = "myusername@domain";
$ldap_connection = ldap_connect("domain");
if (FALSE === $ldap_connection){
    echo "you are not connected to ldap server";
}

ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);

if (TRUE === ldap_bind($ldap_connection, $ldap_username, $ldap_password)){
    $ldap_base_dn = "DC=something,DC=something,DC=something";
    $search_filter = "(&(objectCategory=person)(samaccountname=myusername))";
    $attributes = array();
    $attributes[] = "givenname";
    $attributes[] = "mail";
    $attributes[] = "samaccountname";
    $attributes[] = "sn";
    $result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter, $attributes);
    if (FALSE !== $result){
        $entries = ldap_get_entries($ldap_connection, $result);
        for ($x=0; $x<$entries['count']; $x++){
            if (!empty($entries[$x]['givenname'][0]) || 
                !empty($entries[$x]['mail'][0]) || 
                !empty($entries[$x]['samaccountname'][0]) || 
                !empty($entries[$x]['sn'][0])
                ){
                $ad_users[strtoupper(trim($entries[$x]['samaccountname'][0]))] = array('email' => strtolower(trim($entries[$x]['mail'][0])),'first_name' => trim($entries[$x]['givenname'][0]),'last_name' => trim($entries[$x]['sn'][0]));
            }
        }
    }
    ldap_unbind($ldap_connection); 
}

echo "Retrieved ". count($ad_users) ." Active Directory users\n";

?>

【问题讨论】:

    标签: php ldap


    【解决方案1】:

    您确定属性名为mail?当您使用像 ldapsearch 这样的“已知工作工具”查询 LDAP 时,您会得到什么?在类似 *nix 的系统上,您应该能够使用以下命令查询 LDAP:

    ldapsearch -h domain -b DC=something,DC=something,DC=something "(&(objectCategory=person)(samaccountname=myusername))" givenname,mail,samaccountname,sn
    

    你得到了预期的结果吗?结果是否包含mail 的条目?

    或者,您可以插入var_dump($entries[$x]) 作为for-循环的第一项,以查看是否设置了mail-属性。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多