ManiMuthuPandi ManiMuthuPandi - 25 days ago 11
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.

<?php
$hostname="ldap://mydomain";
$ds=ldap_connect($hostname);
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)
{
$dn="DC=mydomain,DC=mydomain.net";

$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>";
die;
}
$filter = "(samaccountname=mani*)";
$attributes =array('mail');//array("name","telephonenumber","mail","samaccountname");

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

ldap_unbind($ds);
?>


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

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.

Comments