为了实现sso统一认证,将以前的系统认证方式换了,修改密码的模块也就得相应的改变。

 

 
  
  1. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  3. <head> 
  4. <title> Reset LDAP Password</title> 
  5. <style type="text/css"
  6. dt{ 
  7.    font-weight: bold; 
  8. </style> 
  9. </head> 
  10. <body> 
  11. <?php 
  12. /* 
  13. *PHP写的修改LDAP密码的小页面,要先通过用户cn获取到dn,然后通过dn修改用户的密码。
  14. */ 
  15.  
  16. if(isset($_REQUEST) && array_key_exists('an',$_REQUEST)){ 
  17. $u  = $_REQUEST['u'];  //用户名 
  18. $op = $_REQUEST['op']; //旧密码 
  19. $np1 = $_REQUEST['np1']; 
  20. $np2 = $_REQUEST['np2']; 
  21. $an = $_REQUEST['an']; 
  22.  
  23. if(!emptyempty($an)){ 
  24.    ifemptyempty($uor emptyempty($opor emptyempty($np1or emptyempty($np2) ){ 
  25.        $msg = "Some filed was empty!"
  26.    }else
  27.        if$np1 != $np2 ){ 
  28.            $msg = "新密码输入不一致!"
  29.        }else
  30.            if($op == $np1){ 
  31.               $msg = "新密码与旧密码相同,请重新输入!"
  32.            }else
  33.                $ldap_host = "192.168.1.X"
  34.                $ldap_port = 389; 
  35.                $base_dn  = "dc=XXXX,dc=com"
  36.                $connect = ldap_connect( $ldap_host$ldap_port);             //连接服务器 
  37.   if(!$connect){ 
  38.                    $msg = "无法连接LDAP服务器"
  39.                }else
  40.                          $user_pass = $op
  41.                          ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3); 
  42.                          ldap_set_option($connect, LDAP_OPT_REFERRALS, 0); 
  43.                          $uid=sprintf("uid=%s",$u); 
  44.                          $search=ldap_search($connect,$base_dn,$uid);  //根据uid获取到用户的信息 
  45.                          $dn=ldap_get_entries($connect,$search); 
  46.                          for ($i=0; $i<$dn["count"]; $i++)              //从获取到的数组取出用户dn,没有用户dn修改不了密码。
  47.                          { 
  48.                               $user_dn$dn[$i]["dn"]; 
  49.                          } 
  50.                          $bind = ldap_bind($connect$user_dn$user_pass);       //登录验证 
  51.                         if(!$bind){ 
  52.                               $msg = "旧密码不正确,请重新输入!"
  53.                          }else
  54.                                  $values["userPassword"][0] = "{SHA}".base64_encode(pack("H*",sha1($np1)));  //密码sha1加密 
  55.                                  $rs = ldap_mod_replace($connect,$user_dn,$values);           //更新用户信息 
  56.                                 if($rs){ 
  57.                                          $msg="修改成功!"
  58.                                  }else
  59.                                          $msg = "修改失败,请与XX联系!"
  60.                                       } 
  61.                               } 
  62.   
  63.                     } 
  64.                  ldap_close($connect);                                                      //关闭连接 
  65.                  } 
  66.             } 
  67.       } 
  68.   
  69. if(!emptyempty($msg)){ 
  70.    print("<h1>$msg</h1>"); 
  71. ?> 
  72.   
  73. <form  method="post" action=""
  74.  <dl> 
  75.    <dt>用户名</dt> 
  76.  <dd><input type="text" name="u"  /></dd> 
  77.   
  78.    <dt>原密码</dt> 
  79.    <dd><input type="password" name="op" /></dd> 
  80.   
  81.    <dt>新密码</dt> 
  82.    <dd><input type="password" name="np1" /></dd> 
  83.   
  84.    <dt>确认新密码</dt> 
  85.    <dd><input type="password" name="np2" /></dd> 
  86.    <dd><input type="submit" value="确定" /></dd> 
  87.  </dl> 
  88.  <input type="hidden" name="an" value="submit" /> 
  89. </form> 
  90. </body> 
  91. </html> 
  92.   
  93.