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 c++图Djikstra算法求最短路径
    • ¥15 Linux操作系统中的,管道通信问题
    • ¥15 ansible tower 卡住
    • ¥15 等间距平面螺旋天线方程式
    • ¥15 通过链接访问,显示514或不是私密连接
    • ¥100 系统自动弹窗,键盘一接上就会
    • ¥50 股票交易系统设计(sql语言)
    • ¥15 调制识别中这几个数据集的文献分别是什么?
    • ¥15 使用c语言对日志文件处理
    • ¥15 请大家看看报错原因,为啥会这样