I need to write a script to get some user info in an LDAP server. This works well if I dont filter anything, but if I set filter to some specific user, the result always 0. I suspect the filter string, but not sure about that.
Here is the code:
<?php
// using ldap bind
$ldaprdn = 'uid=riemann,dc=example,dc=com'; // ldap rdn or dn
$ldappass = 'password'; // associated password
// connect to ldap server
$ldapconn = ldap_connect("ldap.forumsys.com")
or die("Could not connect to LDAP server.");
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
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...";
}
}
$attributes = array('mail');
$accountname = "*"; //fine if =*, 0 if set it = riemann
$filter_person = "(&(sAMAccountName={$accountname}))";
echo $filter_person;
$search = ldap_search($ldapconn,"DC=example,DC=com", $filter_person, $attributes);
$data = ldap_get_entries($ldapconn, $search);
print_r($data);
?>
I figured it out. It's my fault that I set $filter_person wrong.
It should be:
$filter_person = "uid={$accountname}";