Connect to LDAP anonymously or with a uid/pass combo


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. // basic sequence with LDAP is connect, bind, search, interpret search
  3. // result, close connection
  4.  
  5. // using ldap bind
  6. $ldaprdn = 'userid'; // ldap rdn or dn
  7. $ldappass = 'password'; // associated password
  8.  
  9. echo "<h3>LDAP query test</h3>";
  10. echo "Connecting ...";
  11. // connect to ldap server
  12. $ldapconn = ldap_connect("server_address")
  13. or die("Could not connect to LDAP server.");
  14. // Set some ldap options for talking to
  15. ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
  16. ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
  17.  
  18. echo "connect result is " . $ldapconn . "<br />";
  19.  
  20. if ($ldapconn) {
  21. echo "Binding ...";
  22. $r=ldap_bind($ldapconn,$ldaprdn,$ldappass); // this is an "anonymous" bind, typically
  23. // read-only access
  24. echo "Bind result is " . $r . "<br />";
  25.  
  26. echo "Searching for (sn=S*) ...";
  27. // Search surname entry
  28. $sr=ldap_search($ldapconn, "dc=lakecoe,dc=org", "sn=*");
  29. echo "Search result is " . $sr . "<br />";
  30.  
  31. echo "Number of entires returned is " . ldap_count_entries($ldapconn, $sr) . "<br />";
  32.  
  33. echo "Getting entries ...<p>";
  34. $info = ldap_get_entries($ldapconn, $sr);
  35. echo "Data for " . $info["count"] . " items returned:<p>";
  36. echo print_r($info[0]);
  37. /*for ($i=0; $i<$info["count"]; $i++) {
  38.   //echo "dn is: " . $info[$i]["dn"] . "<br />";
  39.   echo "first cn entry is: " . $info[$i]["cn"][0] . "<br />";
  40.   echo "first email entry is: " . $info[$i]["mail"][0] . "<br /><hr />";
  41.   }*/
  42.  
  43. echo "Closing connection";
  44. ldap_close($ldapconn);
  45.  
  46. } else {
  47. echo "<h4>Unable to connect to LDAP server</h4>";
  48. }
  49. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.