【发布时间】: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";
?>
【问题讨论】: