note 17932 added to function.ldap-bind
| From: | rksmith2 at earthlink dot net | Date: | Thu, 03 Jan 2002 22:46:03 +0000 |
| Subject: | note 17932 added to function.ldap-bind | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-24374@lists.php.net to get a copy of this message | ||
It took quite a while to figure out how to do LDAP authentication as there wasn't a complete
example ... just some cryptic notes about passwords. So, here's what I came up with that works
for me:
// $inp_uid contains the user id to be authenticated
// $inp_passwd contains the plain text password to be authenticated
$ds=ldap_connect("ldap.someserver.com");
//substitute the real host name in the previous statement
if ($ds) {
$r=@ldap_bind($ds); // this is an anonymous bind
$st_search="uid=$res_uid";
// need to set the right root search information in next statement.
// Your requirement may be different
$sr=ldap_search($ds,"ou=mycompany.com,o=My Company", "$st_search");
$info = ldap_get_entries($ds, $sr);
for ($i=0; $i<$info["count"]; $i++) {
$dn=$info[$i]["dn"];
}
// I now know the dn needed to authenticate
// now bind to see if the uid and password work
// the password is still plain text
$r=@ldap_bind($ds, $dn, $inp_passwd);
if ($r) {
$str_passok="Yes";
// ldap_bind will return TRUE if everything matches
} else {
$str_passok="No";
// otherwise ldap_bind will return FALSE
}
ldap_close($ds);
} else {
$error_string="Error -- unable to connect to ldap.someserver.com";
}
I'm sure that there's more error checking that needs to be done, but this provides the
basic skeleton....
--
http://www.php.net/manual/en/function.ldap-bind.php
http://master.php.net/manage/user-notes.php?action=edit+17932
http://master.php.net/manage/user-notes.php?action=delete+17932
http://master.php.net/manage/user-notes.php?action=reject+17932