dsw7547 2016-05-30 14:37
浏览 105
已采纳

ldap搜索PHP后未定义的范围

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.

  • 写回答

1条回答 默认 最新

  • dongzhuang1923 2016-05-31 05:17
    关注

    I'm pretty sure your filter is quirky. From what I read there your filter actually looks this for a user with username janedoe:

    (sAMAccountName={janedoe})
    

    I'd remove the curly brackets and see whether it then works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵