php 修改LDAP 使用者 密碼
<html xmlns="HTTP://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<meta HTTP-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title></title>
</head>
<body>
<!-----------------------修改密碼----------------------------------------------------------------------->
<?php
if(isset($_POST) && array_key_exists('an',$_POST)){
$u = $_POST['u']; //使用者名
$op = $_POST['op']; //舊密碼
$np1 = $_POST['np1'];//新密碼
$np2 = $_POST['np2'];//確認新密碼
$an = $_POST['an'];//送出判斷
}
if(!empty($an)){
if( empty($u) or empty($op) or empty($np1) or empty($np2) ){
$msg = "一些表格空的!";
}else{
if( $np1 != $np2 ){
$msg = "新密碼輸入不一致!";
}else{
if($op == $np1){
$msg = "新密碼與舊密碼相同,請重新輸入!";
}else{
$ldap_host = "xxx.xxx.xxx.xxx";
$ldap_port = 389;
$base_dn = "dc=ntpu,dc=edu,dc=tw";
$connect = ldap_connect( $ldap_host, $ldap_port); //連接伺服器
if(!$connect){
$msg = "無法連接LDAP伺服器";
}else{
$user_pass = $op;
ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
$cn=sprintf("cn=%s",$u);
$search=ldap_search($connect,$base_dn,$cn);//根據cn獲取到使用者的資訊
$dn=ldap_get_entries($connect,$search);
for ($i=0; $i<$dn["count"]; $i++) //從獲取到的陣列取出使用者dn,沒有使用者dn修改不了密碼。
{
$user_dn= $dn[$i]["dn"];
}
$bind = @ldap_bind($connect, $user_dn, $user_pass); //登錄驗證
if(!$bind){
$msg = "舊密碼不正確,請重新輸入!";
}else{
//加密方式有 MD5 or SHA
//$values["userPassword"][0] = "{MD5}" . base64_encode(pack("H*",md5($np1)));
$values["userPassword"][0] = "{SHA}".base64_encode(pack("H*",sha1($np1))); //密碼sha1加密
$rs = ldap_mod_replace($connect,$user_dn,$values); //更新使用者資訊
if($rs){
$msg="修改成功!";
}else{
$msg = "修改失敗!";
}
}
}
ldap_close($connect);// 關閉連接
}
}
}
}
if(!empty($msg)){
print("<h1>$msg</h1>");
}
?>
<!---------------------------------------------------------------------------------------------------->
<form method="post" action="">
<dl>
<dt>帳號</dt>
<dd><input type="text" name="u" /></dd>
<dt>原密碼</dt>
<dd><input type="password" name="op" /></dd>
<dt>新密碼</dt>
<dd><input type="password" name="np1" /></dd>
<dt>確認新密碼</dt>
<dd><input type="password" name="np2" /></dd>
<dd><input type="submit" value="確定" /></dd>
</dl>
<input type="hidden" name="an" value="submit" />
</form>
</body>
</html>
<meta HTTP-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title></title>
</head>
<body>
<!-----------------------修改密碼----------------------------------------------------------------------->
<?php
if(isset($_POST) && array_key_exists('an',$_POST)){
$u = $_POST['u']; //使用者名
$op = $_POST['op']; //舊密碼
$np1 = $_POST['np1'];//新密碼
$np2 = $_POST['np2'];//確認新密碼
$an = $_POST['an'];//送出判斷
}
if(!empty($an)){
if( empty($u) or empty($op) or empty($np1) or empty($np2) ){
$msg = "一些表格空的!";
}else{
if( $np1 != $np2 ){
$msg = "新密碼輸入不一致!";
}else{
if($op == $np1){
$msg = "新密碼與舊密碼相同,請重新輸入!";
}else{
$ldap_host = "xxx.xxx.xxx.xxx";
$ldap_port = 389;
$base_dn = "dc=ntpu,dc=edu,dc=tw";
$connect = ldap_connect( $ldap_host, $ldap_port); //連接伺服器
if(!$connect){
$msg = "無法連接LDAP伺服器";
}else{
$user_pass = $op;
ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
$cn=sprintf("cn=%s",$u);
$search=ldap_search($connect,$base_dn,$cn);//根據cn獲取到使用者的資訊
$dn=ldap_get_entries($connect,$search);
for ($i=0; $i<$dn["count"]; $i++) //從獲取到的陣列取出使用者dn,沒有使用者dn修改不了密碼。
{
$user_dn= $dn[$i]["dn"];
}
$bind = @ldap_bind($connect, $user_dn, $user_pass); //登錄驗證
if(!$bind){
$msg = "舊密碼不正確,請重新輸入!";
}else{
//加密方式有 MD5 or SHA
//$values["userPassword"][0] = "{MD5}" . base64_encode(pack("H*",md5($np1)));
$values["userPassword"][0] = "{SHA}".base64_encode(pack("H*",sha1($np1))); //密碼sha1加密
$rs = ldap_mod_replace($connect,$user_dn,$values); //更新使用者資訊
if($rs){
$msg="修改成功!";
}else{
$msg = "修改失敗!";
}
}
}
ldap_close($connect);// 關閉連接
}
}
}
}
if(!empty($msg)){
print("<h1>$msg</h1>");
}
?>
<!---------------------------------------------------------------------------------------------------->
<form method="post" action="">
<dl>
<dt>帳號</dt>
<dd><input type="text" name="u" /></dd>
<dt>原密碼</dt>
<dd><input type="password" name="op" /></dd>
<dt>新密碼</dt>
<dd><input type="password" name="np1" /></dd>
<dt>確認新密碼</dt>
<dd><input type="password" name="np2" /></dd>
<dd><input type="submit" value="確定" /></dd>
</dl>
<input type="hidden" name="an" value="submit" />
</form>
</body>
</html>
留言
張貼留言