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.

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?