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 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡