ManiMuthuPandi ManiMuthuPandi - 1 year ago 103
PHP Question

Unable to retrieve logged users info in LDAP php

I am using LDAP for users login.
Below code is working fine for validating username and password.

ldap_set_option ($ds, LDAP_OPT_REFERRALS, 0) or die('Unable to set LDAP opt referrals');
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
ldap_set_option($ds, LDAP_OPT_SIZELIMIT, 33);
if ($ds)

$username ="username";
$password ="xxxxxxx";
$ldaprdn = 'VCS' . "\\" . $username;

if (!($ldapc=ldap_bind($ds, $ldaprdn, $password))){
echo "<p>Error:" . ldap_error($ds) . "</p>";
echo "<p>Error number:" . ldap_errno($ds) . "</p>";
echo "<p>Error:" . ldap_err2str(ldap_errno($ds)) . "</p>";
$filter = "(samaccountname=mani*)";
$attributes =array('mail');//array("name","telephonenumber","mail","samaccountname");

$result = ldap_search($ds, $dn, $filter,$attributes);
$info = ldap_get_entries($ds, $result);
for ($i=0; $i < $info["count"]; $i++) {
echo $info[$i]["ou"][0];
} else {
echo "<h4>Unable to connect to LDAP server</h4>";


But it does not return the logged user info.
The value of $info["count"] is always 0.

I think I am misunderstanding the value of base_dn, and filter in ldap_search().

Any suggestion would be great.

Answer Source

Finally I found the solution for my problem.

The filtering is not worked.

So I changed the filter to $filter = "(samaccountname=*)";

Now I can retrieve all the records, from the records I can filter more.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download