我想通过LDAP接收用户的给定名称(displayname也是)。ldap搜索后未定义的范围PHP
我使用下面的代码:
$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");
}
每当我登录,在欢迎页面,cookie的“名”是空的,所以我删除了重定向,看看发生了什么。
现在,我收到以下错误:
Notice: Undefined offset: 0 in C:\wamp\www\OperPHP\authentication.php on line 21
观察:第21行是:
$givenname = $entries[0]['givenname'][0];
我猜东西在我的LDAP查询,但我不知道那里可以是。
任何人都可以帮忙吗?
@EDIT
当我使用echo print_r($entries);
它显示Array ([count] => 0) 1
!这意味着我的$entries
没有收到任何价值。
什么'echo print_r($ entries);'return? –
它显示'Array([count] => 0)1'!!我不太明白这是什么意思...... –
这可能意味着你的代码有问题。你试图得到'$ entries [0] ['givenname'] [0]',但你能从这个变量中获得的唯一东西是'$ entries ['count']'(这个数组唯一的东西存在)将返回'0'。 –