Skip to content

Instantly share code, notes, and snippets.

@codescribblr
Created February 13, 2014 21:41
Show Gist options
  • Save codescribblr/8984431 to your computer and use it in GitHub Desktop.
Save codescribblr/8984431 to your computer and use it in GitHub Desktop.
Connect to LDAP anonymously or with a uid/pass combo
<?php
// basic sequence with LDAP is connect, bind, search, interpret search
// result, close connection
// using ldap bind
$ldaprdn = 'userid'; // ldap rdn or dn
$ldappass = 'password'; // associated password
echo "<h3>LDAP query test</h3>";
echo "Connecting ...";
// connect to ldap server
$ldapconn = ldap_connect("server_address")
or die("Could not connect to LDAP server.");
// Set some ldap options for talking to
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
echo "connect result is " . $ldapconn . "<br />";
if ($ldapconn) {
echo "Binding ...";
$r=ldap_bind($ldapconn,$ldaprdn,$ldappass); // this is an "anonymous" bind, typically
// read-only access
echo "Bind result is " . $r . "<br />";
echo "Searching for (sn=S*) ...";
// Search surname entry
$sr=ldap_search($ldapconn, "dc=lakecoe,dc=org", "sn=*");
echo "Search result is " . $sr . "<br />";
echo "Number of entires returned is " . ldap_count_entries($ldapconn, $sr) . "<br />";
echo "Getting entries ...<p>";
$info = ldap_get_entries($ldapconn, $sr);
echo "Data for " . $info["count"] . " items returned:<p>";
echo print_r($info[0]);
/*for ($i=0; $i<$info["count"]; $i++) {
//echo "dn is: " . $info[$i]["dn"] . "<br />";
echo "first cn entry is: " . $info[$i]["cn"][0] . "<br />";
echo "first email entry is: " . $info[$i]["mail"][0] . "<br /><hr />";
}*/
echo "Closing connection";
ldap_close($ldapconn);
} else {
echo "<h4>Unable to connect to LDAP server</h4>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment