Advertisement
Guest User

PHP LDAP

a guest
Apr 13th, 2019
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.72 KB | None | 0 0
  1. <?php
  2. set_time_limit(30);
  3. error_reporting(E_ALL);
  4. ini_set('error_reporting', E_ALL);
  5. ini_set('display_errors',1);
  6.  
  7. // config
  8. $ldapserver = 'www.zflexldap.com';
  9. $ldapuser   = 'uid=guest2,ou=users,ou=guests,dc=zflexsoftware,dc=com';  
  10. $ldappass   = 'guest2password';
  11. $ldaptree   = "dc=zflexsoftware,dc=com";
  12.  
  13. // connect
  14. $ldapconn = ldap_connect($ldapserver) or die("Could not connect to LDAP server.");
  15.  
  16. if($ldapconn) {
  17.     // binding to ldap server
  18.     $ldapbind = ldap_bind($ldapconn, $ldapuser, $ldappass) or die ("Error trying to bind: ".ldap_error($ldapconn));
  19.     // verify binding
  20.     if ($ldapbind) {
  21.         echo "LDAP bind successful...<br /><br />";
  22.        
  23.        
  24.         $result = ldap_search($ldapconn,$ldaptree, "(uid=guest2)") or die ("Error in search query: ".ldap_error($ldapconn));
  25.         $data = ldap_get_entries($ldapconn, $result);
  26.        
  27.         // SHOW ALL DATA
  28.         echo '<h1>Dump all data</h1><pre>';
  29.         print_r($data);    
  30.         echo '</pre>';
  31.        
  32.        
  33.         // iterate over array and print data for each entry
  34.         echo '<h1>Show me the users</h1>';
  35.         for ($i=0; $i<$data["count"]; $i++) {
  36.             //echo "dn is: ". $data[$i]["dn"] ."<br />";
  37.             echo "User: ". $data[$i]["cn"][0] ."<br />";
  38.             if(isset($data[$i]["mail"][0])) {
  39.                 echo "Email: ". $data[$i]["mail"][0] ."<br /><br />";
  40.             } else {
  41.                 echo "Email: None<br /><br />";
  42.             }
  43.         }
  44.         // print number of entries found
  45.         echo "Number of entries found: " . ldap_count_entries($ldapconn, $result);
  46.     } else {
  47.         echo "LDAP bind failed...";
  48.     }
  49.  
  50. }
  51.  
  52. // all done? clean up
  53. ldap_close($ldapconn);
  54. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement