php - LDAP login page doesn't work -
i installed php on iis , tested script, there no response after either enter correct or incorrect login details. try telnet ldap server , connect properly.
<?php $ldapserver = "ldap.server.com"; // ldap server $ldapsuffix = "o=companyname, o=companynet"; // ldap tree if (!empty($_post['login'])) { //print_r($_post); echo "<br><br>"; $userid = $_post['username']; // user key userid or email $userpassword = $_post['userpassword']; // user key password $ds=ldap_connect($ldapserver); ldap_set_option($ds, ldap_opt_protocol_version, 3); $bind=@ldap_search($ds, $ldapsuffix, "uid=".$userid); if ($bind) { echo "ldap bind success <br>"; $result = @ldap_get_entries($ds, $bind); if ($result[0]) { if (@ldap_bind( $ds, $result[0]['dn'], $userpassword)) { echo "user bind success <br>"; // can proceed check database } else { echo "user bind failed - invalid password <br>"; } } else { echo "user not has ldap account <br>"; } } else { echo "ldap bind failed <br>"; } ldap_close($ds); } ?> <html> <head> <title>login ldap authentication</title> </head> <body> <form action="ldaplogin.php" name="passwordchange" method="post"> <table style="width: 400px; margin: 0 auto;"> <tr><th>enter username/email:</th><td><input name="username" type="text" size="0" autocomplete="off" /></td></tr> <tr><th>enter password:</th><td><input name="userpassword" size="20" type="password" /></td></tr> <tr><td colspan="2" style="text-align: center;" > <input name="login" type="submit" value="login"/></td></tr> </table> </form> </div> </body> </html>
you need ldap_bind()
prior being able ldap_search()
. remove @
prepend functions can see errors, warnings, , notices.
edit: should close need, , can adjusted fit need. assumes ldap server not allow anonymous searches, in case does, can rearrange things.
$ldapserver = "ldap.server.com"; // ldap server $ldapsuffix = "o=companyname, o=companynet"; // ldap tree if (!empty($_post['login'])) { echo "<br><br>"; $userid = $_post['username']; // user key userid or email $userpassword = $_post['userpassword']; // user key password $ds = ldap_connect($ldapserver); ldap_set_option($ds, ldap_opt_protocol_version, 3); $bind = @ldap_bind($ds, "uid=$userid,$ldapsuffix", $userpassword); if ($bind !== false) { $search = @ldap_search($ds, $ldapsuffix, "uid=$userid", array('uid', 'dn')); if ($search !== false) { $result = @ldap_get_entries($ds, $search); if (is_array($result) , $result) { // can proceed check database } elseif ($result === array()) { echo "no results"; } elseif ($result === false) { echo "ldap_get_entries() failed: " . ldap_error($ds); } } else { echo "ldap_search() failed: " . ldap_error($ds); } } else { echo "ldap_bind() failed: " . ldap_error($ds); } @ldap_unbind($bind); @ldap_close($ds); }
Comments
Post a Comment