ScotlandPHP

ldap_get_entries

(PHP 4, PHP 5, PHP 7)

ldap_get_entriesObtener todas las entradas de los resultados

Descripción

array ldap_get_entries ( resource $link_identifier , resource $result_identifier )

Lee múltiples entradas de los resultados porporcionados, luego lee los atributos y valores múltiples.

Parámetros

link_identifier

Un enlace identificador de LDAP, devuelto por ldap_connect().

result_identifier

Valores devueltos

Devuelve un resultado completo de información en un array multi-dimensional en caso de éxito y FALSE en caso de error.

La estructura del array es como sigue. El índice del atributo se convierte a minúsculas. (Los atributos son insensibles a mayúsculas o minúsculas para los servidores de directorio, pero no cuando se usan como índices de array.)

return_value["count"] = número de entradas en el en el resultado
return_value[0] : se refiere a los detalles de la primer entrada

return_value[i]["dn"] =  DN de la i-ésima en el resultado

return_value[i]["count"] = Número de atributos en la i-ésima entrada
return_value[i][j] = NOMBRE del j-ésimo atributo en la i-ésima entrada en el resultado

return_value[i]["attribute"]["count"] = número de valores para
                                        el atributo en la i-ésima entrada
return_value[i]["attribute"][j] = j-ésimo valor del atributo en la i-ésima entrada

Ver también

add a note add a note

User Contributed Notes 17 notes

up
6
tomas dot hampl at gmail dot com
5 years ago
It has been mentioned in the comments below, but the <?php ldap_get_entries($connection,$result) ?> always makes all attributes into lower-case. I found out the hard way that if I'm running an ldap query against the corporate AD and then want to display some results, nothing is actually displayed unless the attribute names area in lower-case.

Example:

<?php
// connect to AD
include('ad_con.php');
// limit attributes we want to look for
$attributes_ad = array("displayName","description","cn","givenName","sn","mail","co","mobile","company","displayName");
// define base
$base ="";

// in my script I search based on e-mail, $email variable is passed from the form
$result = ldap_search($conn, $base, "mail=$email*", $attributes_ad) or die ("Error in search
        query"
);

// put search results into the array ($conn variable is defined in the included 'ad_con.php')
$info = ldap_get_entries($conn, $result);

//Now, to display the results we want:
for ($i=0; $i<$info["count"]; $i++)
    {
   
// to show the attribute displayName (note the case!)
   
echo $info[$i]["displayname"][0]
   
    }

?>
up
1
Sebastien Troiani
7 years ago
Looks like there is a limit on returned objects - only 1000 items are placed in the array
up
2
mackstann / mack%at%incise%dot%org
14 years ago
I find the ["count"] items in these arrays highly annoying, so I made a function to remove them recursively:

function rCountRemover($arr) {
  foreach($arr as $key=>$val) {
    # (int)0 == "count", so we need to use ===
    if($key === "count")
      unset($arr[$key]);
    elseif(is_array($val))
      $arr[$key] = rCountRemover($arr[$key]);
  }
  return $arr;
}
up
1
reaper at sci dot fi
14 years ago
If you're dealing with Active Directory and need to get values like 'lastlogon', 'pwdlastset' or similar, you'll notice that AD gives the values as Windows FILETIME timestamps. That means, the values are 100-nanosecond units passed since 1.1.1600 00:00:00.

To convert these to unix timestamps which PHP's date functions understand, one easy way would be the following :

function win_filetime_to_timestamp ($filetime) {

  $win_secs = substr($filetime,0,strlen($filetime)-7); // divide by 10 000 000 to get seconds
  $unix_timestamp = ($win_secs - 11644473600); // 1.1.1600 -> 1.1.1970 difference in seconds
  return $unix_timestamp;
}
up
0
chl
8 years ago
Recursive form of markus' function so it can take directly the result of ldap_get_entries :
<?php
function cleanUpEntry( $entry ) {
 
$retEntry = array();
  for (
$i = 0; $i < $entry['count']; $i++ ) {
    if (
is_array($entry[$i])) {
     
$subtree = $entry[$i];
     
//This condition should be superfluous so just take the recursive call
      //adapted to your situation in order to increase perf.
     
if ( ! empty($subtree['dn']) and ! isset($retEntry[$subtree['dn']])) {
       
$retEntry[$subtree['dn']] = cleanUpEntry($subtree);
      }
      else {
       
$retEntry[] = cleanUpEntry($subtree);
      }
    }
    else {
     
$attribute = $entry[$i];
      if (
$entry[$attribute]['count'] == 1 ) {
       
$retEntry[$attribute] = $entry[$attribute][0];
      } else {
        for (
$j = 0; $j < $entry[$attribute]['count']; $j++ ) {
         
$retEntry[$attribute][] = $entry[$attribute][$j];
        }
      }
    }
  }
  return
$retEntry;
}
?>

Result is of the form :
array(256) {
  ["uid=doe,ou=People,dc=example,dc=net"]=>
  array(3) {
    ["uid"]=>
    string(4) "doe"
    ["cn"]=>
    string(14) "John Doe"
    ["telephonenumber"]=>
    string(4) "1234"
  }
  ["uid=foo,ou=People,dc=example,dc=net"]=>
  ...
up
0
markus dot schabel at tgm dot ac dot at
11 years ago
When you like to get the entry from LDAP in the same style as ldap_add(), then you can use the following function to convert this entry.

<?php
/**
* Take an LDAP and make an associative array from it.
*
* This function takes an LDAP entry in the ldap_get_entries() style and
* converts it to an associative array like ldap_add() needs.
*
* @param array $entry is the entry that should be converted.
*
* @return array is the converted entry.
*/
function cleanUpEntry( $entry ) {
   
$retEntry = array();
    for (
$i = 0; $i < $entry['count']; $i++ ) {
       
$attribute = $entry[$i];
        if (
$entry[$attribute]['count'] == 1 ) {
           
$retEntry[$attribute] = $entry[$attribute][0];
        } else {
            for (
$j = 0; $j < $entry[$attribute]['count']; $j++ ) {
               
$retEntry[$attribute][] = $entry[$attribute][$j];
            }
        }
    }
    return
$retEntry;
}
?>
up
0
reuben dot helms at gmail dot com
11 years ago
Helmass=bsp; $d> /spas="keyword">[$i];
    &nEntry<3(iiten clasclass=
egant T{
br />  cla &nor" hrup
down
5415
11 years ago
Helmass=bsp; /spas="keyword">[];
   And="lamesta &nigno< claed en8planull lsp; mp = ($wino an clarazile,
Non-Te de3] " title="2et($arr[$key])or($inbs0; $inlass=de3] (ect1 paror" h) - 1; $i$entry v ifar morean>egantamp;{ted. Idiv>yry.pm" titl&kis >) up
down
11 years ago
Helmass=bsp;     # (int)0hass="kut to ="dntry.
*/
/spa"ge)unontentry.
*/
ult""20"de3] wan>es="kes="gendi=0;s="gendiónenbs_entry() - Ob$e="2ote.p($entry$entry
pan class="keyword">[$entry[[$entry< }cl="defwhion<(diónenbs_entrp-get-entri$e="2ote.p( - Op180"> < up
down
1
11 years ago
It has been mentioned in the comments bel8916"> to get values like 'lastlogon', 'pwdla2"9or s4úmero de atan class="keyword">($connec8916"> pan>];
   A
filetifarazo ="dntry.
*/ n>otbpup
down
1
11 years ago
It has been mentioned in the comments bel4844"> to get values like 'lastlogon', 'pwdla1-08-17 04:25mero de at6n class="keyword">($connec4844"> r as $key=>$val) {
    # (int)dice pontime)-letif clas/spfere;ntry.
*/inkote.php?idn cla &nfuataba.1.16="Vod> < 0"> < 0"> es="k" 0"> < 0"> $ioa.1.16="Vosp; verto gtimerr, ra="lamletifarazo ="d="Vodnn?id=3s < 0"> incl titl: 0"> < 0"> <$me_tonbs
< 0"> < 0"> <$me_tonbs
r /d=foo, $de5541&er)o:/$foo;s" class="usernotes-voteu">up
1
down
down" title="Vote down!" class="usernotes-votas dot hampl at gmail dot com " title="2006-02-07 01:13">11 years ago "iv class="text" id="Hcom61583">
inuxclass>inclassnotml"> It has been mentioned in the comments bel29> to get values like 'lastlogon', 'pwdla1-05-tan09:5 ero de at6n class="keyword">($connec29> r as $key=>$val) {
    # (intNcla:;ntry.
*/ < 0"> <="def$/> */ */ < 0"> < 0"> e &nrowverted eAan class="e="2e 0"> < 0"> */ < 0"> up
down
11 years ago
It has been mentioned in the comments belo2code to get values like 'lastlogon', 'pwdla0-12-20ntri24 ero de at6n class="keyword">($connecs2code r as $key=>$val) {
    # (intJ&nass="o) {
incl titldc=="tri"kees&aicmes&aorg 0"> < 0"> es="keyo ="dimesbu"usd/.1.16i) $unix>es="k;  o g/> {
< 0"> es="k>(es="k>(up
down
It has been mentioned in the comments be4176 to get values like 'lastlogon', 'pwdla4-a4-21 03:57ilar, you'l3 notice that AD gives the values as Windows FILETIME t4176 s. That means, the values are 100-nanosecond units paclasp Pvlamleti/> */ !0">  "> <; */    &nbs6is_null(f$/> */
up
down
14 years ago
When you like to get the entry from LDAP 7797 to get values like 'lastlogon', 'pwdla7ve t21 02:48ilar, you'l0 notice that AD gives the values as Windows FILETIME t7797 s. That means, the values are 100-nanosecond unS cla &de Iclassto clher. MaybVoy/spmion benefitould be convert
* converts it to an associative array like ldap_add() nan>= array();
 s"keyw_n class(
][]
   
f(is_array($val))
o
[);

aslass="keyword">[
$entry$subtree = $epass="default">$entryo[);

aslass="keyword">[
<>[[$entry[[de3] ree = $t">$retEntry;
}
) - pan>] = /span>$ente.ph/span>["keyword">][$entry$subtree = $epass="default">$entry= $t">$retEntry;
}
bute displayName (note the case!)
   
$attributeclass( $retEntry;
}
nbsp;       }
$t">$retEntry;
}
efault">?>

Result is of the form :
array(256) {
  ["uid=doe,ou=People,dc=exalass="default">$attributeclass( $retEntry
">,
>nbsp;       }
r /ree
= [=]
   
    }

; t">$retEntry
">,>180"> asclard">= ldap_search[$attribute= $e;0"> [; $ilass="keyword">[$attribute[; $silass="keyword">[$attribute$email*""Error in ou=cust clrees&aDOMAINes&v( "Error in pagev( ; $pan class="keyword">);

//Now, to display the results we want:
for ds( "default">$si( ; <"keyword">; ="default">0]
   
    }
e the case!)
   
&n(s)="ke \ord">= = ldord">; <"keyword">; iv id="Vu61583">
);

//s"keyw_n class( ][]
   
fn class="default"r />o
[);

aslass="keyword">[
<>[[0$entry0= ;
}
=
ldord">; ss="key  $subtree = 0$entry
rd">= );

aslass="keyword">[
$entry0= = ldord">; ss="keybr />array(256) {
  ["uid=doe,ou=People,dc=exalass="default">$attribute= [up
down
514ode
11 years ago
{t orixclass="hass="hutml"> It has been mentioned in the comments be514ode to get values like 'lastlogon', 'pwdla0-04-17 10:0úmero de at
    # (intNclazo ="dntry.
*/up
down
90126">
11 years ago
Helmass=bsp; directly the result of ldap_get_entries :4ot;] 9similar, you'efault"><?php
func90126"> pan>];
    op="e="2"cp = (>[$attribute
= [= <"lass="keyword">[$attribute= [0[$attribute[0<"default">$root_dspan class="default">0<"default">$root_pw( = <"lass="keyword">[= [0);

">$attribute
$email*"0<"default">$pershss=l_bas/ree "Error in (page*)v/span>$eP's diebsp; $e;0"> );

//Now, to display the results we want:
for ntry."hspan class="default">0<"default">$ran>0 ][);

][
;class="keyword">[$i][]
   
    }
e the case!)
   
[$i$entry][;
}
][
;css="keyword">][;
}
][
]
   
    }
n>'count'e the case!)
   
[][] = $entry0
]
   
    }
n>'count'pan class="keyword">;
}
.ap-get-entries&ve the cas:/rd">=
]
   
    }
n>'count'pan class=d r">]
   
    }
n>'count'pan class="keyword">;
}
0; .ap-get-entries&ve the cas\ard">= ldord">; ss="keybr />ar=doe,ou=Peopletion.ldap-get-entries&vote=up" tntry.llos/ree = [0up u">updiv>
dimg src='/images/> addaa cla!" mall>ly th">up:20">< ete do>