doufang6268 2012-07-25 19:48
浏览 105
已采纳

PHP属性/关联数组中的未定义索引警告?

I am having trouble retrieving some data out of an LDAP attribute in PHP.

I connect to LDAP, perform my query, and store the results in a var like so:

$info = ldap_get_entries($connect, $sr);

Now, I can store most of the LDAP attributes I need in sessions vars, like so:

        $_SESSION['accountFirstName'] = $info[0]['givenname'][0];
        $_SESSION['accountLastName'] = $info[0]['sn'][0];
        $_SESSION['accountEmail'] = $info[0]['mail'][0];

These work fine.. No problems. However, there is another var I need to store. I believe It is an associative array. For some reason, no matter what I do, I am getting an NOTICE: Undefined index warning for that specific attribute. I have tried storing it like the above demonstration, but to be honest I'm not entirely sure what the [0] indices on either ends of the attribute name mean.. I'm not familiar with LDAP and frankly the setup is very confusing.

So I guess my questions are:

  • how do you access associative arrays that are returned from an LDAP query?

  • what does 'undefined index' mean? Does it mean that that attribute does not exist, or it does not exist at the index provided?

  • How can I test my LDAP query to see if the variable even exists?

  • There is a possibility that the account performing the query does not have adequate LDAP access privileges (the project is for a university and there is a lot of red tape). Is there any way for me to verify that through code?

Thank you! And my apologies for the vagueness of the information provided, I can't be too open-mouthed right now.

  • 写回答

6条回答 默认 最新

  • doucong7963 2012-07-25 19:54
    关注

    The issue doesn't reside with the ldap_get_entries() method, or LDAP at all - it's the data that's being returned.

    The NOTICE: Undefined index error is stating that an index in your array doesn't exist. In this case, it is most likely that the data you're receiving doesn't have a value such as givenname or mail, but it could also be the [0] (or, "first record") in one of those arrays.

    The textual/string index value, such as givenname or sn, is what would be defined as the "associative array". You are correctly accessing that data with $info[0]['givenname'];

    To check if an index exists in PHP, you can use isset(), such as:

    if (isset($info[0]['givenname'])) {
        // process data here
    }
    

    As a quick way to do your assignments, you can use something like this:

    if (count($info) > 0) {
        $_SESSION['accountFirstName'] = (isset($info[0]['givenname']) && isset($info[0]['givenname'][0])) ? $info[0]['givenname'][0] : '';
    }
    

    If you have a long list of values you need, I would suggest writing something similar to the following to auto-process it for you:

    $fields = array(
        'accountFirstName' => 'givenname',
        'accountLastName' => 'sn',
        'accountEmail' => 'mail'
    );
    
    $info = $info[0];
    foreach ($fields as $field => $ldapField) {
        // check if the value exists; otherwise set it to an empty-string
        $_SESSION[$field] = (isset($info[$ldapField]) && isset($info[$ldapField][0])) ? $info[$ldapField][0] : '';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀