簡體   English   中英

PHP LDAP:ldap_search遇到錯誤

[英]PHP LDAP: Error encountered on ldap_search

我在互聯網上獲得了這段代碼,並做了一些小的修改。 我打算通過將他們輸入的用戶名和密碼與我們的廣告相匹配來對我的網站的用戶進行身份驗證。

該代碼似乎可以正常工作,但是當我嘗試添加mail屬性時,出現此錯誤:

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";

?>

您確定該屬性名為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 -loop的第一項,以查看是否設置了mail -Attribute。

希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM