无法使用PHP搜索LDAP服务器

时间:2014-08-26 18:34:00

标签: php active-directory ldap

如果用户存在,我正在尝试搜索Active Directory。我收到以下错误消息。  “无法搜索LDAP服务器”。 可能有什么不对?请建议。

<?php

// LDAP variables
$ldaphost = "servername";  // your ldap servers
$ldapport = 389;                 // your ldap server's port number

// Connecting to LDAP
$ldapconn = ldap_connect($ldaphost, $ldapport)
          or die("Could not connect to $ldaphost");


$user = 'mylastname';

//search user in /Admin/IT/Users

$dn = "OU=Admin, OU=IT, OU=Users,   DC=school, DC=edu";

$filter = "(sAMAccountName=" . $user . ")";
$attr = array("memberof");
$result = ldap_search($ldapconn , $dn, $filter, $attr) or exit("Unable to search LDAP server");
$entries = ldap_get_entries($ldapconn, $result);
echo $entries["count"]." entries returned\n";

?> 

1 个答案:

答案 0 :(得分:2)

如果您正在与Active Directory服务器通信,则应始终将ldap协议版本设置为3并关闭引用处理:

ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);

此外,专有名称的部分不应以空格分隔 - 重新格式化如下:

$dn = "OU=Admin,OU=IT,OU=Users,DC=school,DC=edu";

最后,如果出现问题,请务必检查what the LDAP server says

$result = ldap_search($ldapconn, $dn, $filter, $attr) or exit("Unable to search LDAP server, response was: " . ldap_error($ldapconn));