Skip to content

ldap_search() fails when $attributes array has holes #17280

Closed
@dregad

Description

@dregad

Description

The following code:

<?php
$ldap_server = 'ldap://localhost';
$root_dn = 'ou=people,dc=example,dc=com';

$ldap = ldap_connect( $ldap_server );
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_bind($ldap); // Anonymous bind

// Creating an array with a hole in it
$attr = array_unique(['cn', 'uid', 'uid', 'mail']);
$res = ldap_search($ldap, $root_dn, 'uid=admin', $attr);
echo ldap_error($ldap);

Resulted in this output:

Warning: ldap_search(): Array initialization wrong in ./test.php on line 11

Call Stack:
    0.0012     401536   1. {main}() ./test.php:0
    0.1191     401816   2. ldap_search($ldap = class LDAP\Connection {  }, $base = 'ou=people,dc=example,dc=com', $filter = 'uid=admin', $attributes = [0 => 'cn', 1 => 'uid', 3 => 'mail']) ./test.php:11

Success  

But I expected this output instead:

Success

If $attr has no holes in the keys (i.e. remove array_unique() or call array_values()) then it works fine.

Maybe this is normal, but in that case I believe the documentation should clarify that, as it currently does not say anything about $attributes parameter needing to be a list with keys in a strict sequence.

PHP Version

8.2.27, 8.4.2

Operating System

MacOS 14.6.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions