Home >Backend Development >PHP Problem >How to change password in ldap php
How to change the password in ldap php: first find the form page "ldap.html"; then open the processing logic page "ldap.php"; finally modify the code to "$_POST["new_pass1"];if ( !$old_pass) {return...}".
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php realizes the modification of ldap account password
Form page ldap.html
<html><body><form action="ldap.php" method="post">帐号名: <input type="text" name="acc_name"><br>旧密码: <input type="text" name="old_pass"><br>新密码: <input type="text" name="new_pass"><br> Again: <input type="text" name="new_pass1"><br><input type="submit"></form></body></html>
Processing logic ldap.php
<html> <body> <?php function ChangePass() { $acc_name = $_POST["acc_name"]; $old_pass = $_POST["old_pass"]; $new_pass = $_POST["new_pass"]; $new_pass1 = $_POST["new_pass1"];if (!$old_pass) {return "请输入旧密码。"; }if (!$new_pass || !$new_pass1) {return "请输入新密码。"; }if ($new_pass != $new_pass1) {return "新前后密码不一致!!!"; } $ldap_conn = ldap_connect("ldap://192.168.1.14:389");if (!$ldap_conn) {return "服务器连接失败!!!"; } ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3); $bind = ldap_bind($ldap_conn, "cn=admin,dc=xidea,dc=com", "jcd520");if (!$bind) {return "error"; } $dn = "ou=people,dc=xidea,dc=com"; $res = ldap_search($ldap_conn, $dn, "(uid=".$acc_name.")");if (!$res) {return "账号不存在1"; } $entry = ldap_first_entry($ldap_conn, $res);if (!$entry) {return "账号不存在"; } $attrs = ldap_get_attributes ( $ldap_conn, $entry); $db_pass1 = $attrs['userPassword'][0]; $db_pass2 = '{SHA}' . base64_encode(pack('H*', sha1($old_pass)));if ($db_pass1 != $db_pass2) {return "密码错误"; }return "suc"; $user_dn = ldap_get_dn($ldap_conn, $entry); $new["userPassword"] = '{SHA}' . base64_encode(pack('H*', sha1($new_pass))); ldap_modify($ldap_conn, $user_dn, $new); ldap_close($ldap_conn); }echo ChangePass();echo "<a href=\"ldap.html\">重新修改</a>" ?></body> </html>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to change password in ldap php. For more information, please follow other related articles on the PHP Chinese website!