Net_LDAP
[ class tree: Net_LDAP ] [ index: Net_LDAP ] [ all elements ]

Source for file search_entries.php

Documentation is available at search_entries.php

  1. <?php
  2. /**
  3. * This is a short example on how to search for entries in your
  4. * directory using Net_LDAP.
  5. */
  6.  
  7. // We use the connecting.php example to get a link to our server.
  8. // This file will also include all required basic Net_LDAP classes.
  9. include_once 'connecting.php';
  10.  
  11. // Okay, we should have a valid link now.
  12. // We must now define a filter that defines, which
  13. // entries we want to have returned.
  14. // We use the Net_LDAP_Filter class for this purpose,
  15. // because so we don't need to worry about
  16. // RFC-2254 ;)
  17. // In this example, we want all users with first names
  18. // starting with "bened" and the last names ending with "ger".
  19. // Additionally, we want to exclude all users with names
  20. // containing "smith", which will be done throug a "negation".
  21.  
  22. // Basic filter building
  23. $filter_sn = Net_LDAP_Filter::create('gn', 'begins',  'bened');
  24. $filter_gn = Net_LDAP_Filter::create('sn', 'ends',    'ger');
  25.  
  26. // Building and negating the "no smith" filter component
  27. // this must be done in two steps, because
  28. // you are able to negate EVERY filter, not just leave filters.
  29. // the $filter_smith will not be used afterwards and is only
  30. // necessary for negation here.
  31. $filter_smith   = Net_LDAP_Filter::create('sn', 'contains','smith');
  32. $filter_nosmith = Net_LDAP_Filter::combine('not', $filter_smith);
  33.  
  34. // Now combine all filter components to build our search filter
  35. $filter = Net_LDAP_Filter::combine('and', array($filter_sn, $filter_gn, $filter_nosmith));
  36.  
  37.  
  38. // The filter is ready now, so we can
  39. // use this filter now to search for entries.
  40. // The scope we use is "sub", meaning "all entries below the search base".
  41. // The base is "null", meaning the base defined in $ldap_config. This is similar
  42. // to call $ldap->search($ldap_config['base'], ...
  43. $requested_attributes = array('sn','gn','telephonenumber');
  44. $search = $ldap->search(null, $filter, array('attributes' => $requested_attributes));
  45. if (Net_LDAP::isError($search)) {
  46.     die('LDAP search failed: '.$search->getMessage());
  47. }
  48.  
  49. // Lets see what entries we got and print the names and telephone numbers:
  50. if ($search->count() > 0) {
  51.     echo "Found ".$search->count().' entries:<br>';
  52.  
  53.     // Note, this is is only one of several ways to fetch entries!
  54.     // You can also retrieve all entries in an array with
  55.     //   $entries = $search->entries()
  56.     // or the same thing sorted:
  57.     //   $entries = $search->sorted()
  58.     while ($entry = $search->shiftEntry()) {
  59.         $surename = $entry->getValue('sn', 'single');
  60.         if (Net_LDAP::isError($surename)) {
  61.             die('Unable to get surename: '.$surename->getMessage());
  62.         }
  63.         $givenname = $entry->getValue('gn', 'single');
  64.         if (Net_LDAP::isError($givenname)) {
  65.             die('Unable to get givenname: '.$givenname->getMessage());
  66.         }
  67.         $phone = $entry->getValue('telephonenumber', 'single');
  68.         if (Net_LDAP::isError($phone)) {
  69.             die('Unable to get phone number: '.$phone->getMessage());
  70.         }
  71.         echo "<br>$givenname $surename: $phone";
  72.     }
  73. } else {
  74.     die('Sorry, no entries found!');
  75. }
  76. ?>

Documentation generated on Mon, 11 Mar 2019 15:32:06 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.