I want to receive through LDAP the user's givenname (displayname also).
I am using the following code:
$ldap = ldap_connect("ldap://domain.com");
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
if ($bind = ldap_bind($ldap, $myusername, $mypassword))
{
$_SESSION['login_user'] = $myusername;
$filter = "(sAMAccountName={$myusername})";
$ldap_dn = "dc=domain, dc=com";
$attr = array("givenname");
$result = ldap_search($ldap, $ldap_dn, $filter, $attr) or exit("Unable to search LDAP server");
$entries = ldap_get_entries($ldap, $result);
$givenname = $entries[0]['givenname'][0];
ldap_unbind($ldap);
setcookie("name", $givenname, time() + (86400 * 30), "/");
setcookie("sessao", $local, time() + (86400 * 30), "/"); // 86400 = 1 dia
//header("location: welcome.php");
}
Whenever I login, in the welcome page, the cookie "name" is empty, so I removed the redirect to see what is happening.
Now I am receiving the following error:
Notice: Undefined offset: 0 in C:\wamp\www\OperPHP\authentication.php on line 21
Observation: Line 21 is:
$givenname = $entries[0]['givenname'][0];
I guess is something in my LDAP query, but I don't know where it could be.
Anyone could help?
@EDIT
When I use echo print_r($entries);
it shows Array ( [count] => 0 ) 1
! Which means my $entries
is not receiving any value.