2011-11-10 29 views
0
<? 
// Inialize session 
session_start(); 

// Check, if username session is NOT set then this page will jump to login page 

if (!isset($_SESSION['username'])) 
{ 
header('Location: AdminLogin.php'); 
} 
?> 

<html lang="en-GB" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<link rel="stylesheet" href="AdminLogin.css" type="text/css" /> 
<title>Welcome to ASM Services Inc.</title> 

<script type="text/javascript" language=JavaScript> 
var message=""; 
function clickIE() 
{ 
    if (document.all) 
    {(message);return false;}} 
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) { 
if (e.which==2||e.which==3) {(message);return false;}}} 
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;} 
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;} 

document.oncontextmenu=new Function("return false") 
</script> 
</head> 
<body> 

<div class="login"> 
<?php 
require("adminconfig.inc"); 
$user = $_SESSION['username']; 
echo "<form name=form1 method=post> 
<table width=100 border=0 align=center> 
<tr> 
<font size=5 face=Arial color=yellow>Change Password</font> 
</tr> 
<table> 
    <tr> 
     <td><font size=4 face=Tahoma color=yellow>Username:</font></td> 
     <td><input type=text name='username1' value='$user' size=20 AUTOCOMPLETE = off ></td> 
    </tr> 
    <tr> 
     <td><font size=4 face=Tahoma color=yellow>Password:</font></td> 
     <td><input type=password name=password size=20 AUTOCOMPLETE = off></td> 
    </tr> 
    <tr> 
     <td><font size=4 face=Tahoma color=yellow>New Password</font></td> 
     <td><input type=password name=new_pass size=20 AUTOCOMPLETE = off></td> 
    </tr> 
    <tr> 
     <td><font size=4 face=Tahoma color=yellow>Confirm Password:</font>:</td> 
     <td><input type=password name=con_pass size=20 AUTOCOMPLETE = off></td> 
    </tr> 
</table> 
<table> 
    <tr> 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <input type=submit value=Ok name='btnCheck'> 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <input type=submit value=Cancel name=btnCancel onClick='this.form.reset()'> 
    </tr> 
</table> 
</table> 
</form>"; 
?> 

<?php 
require("adminconfig.inc"); 
$user = $_POST['username1']; 
$pass = $_POST['password']; 
$new_pass = trim($_POST['new_pass']); 
$con_pass = trim($_POST['con_pass']); 
if(isset($_POST['btnCheck'])) 
{ 
// Retrieve username and password from database according to user's input 
$login = mysql_query("SELECT Log_User, Log_Pass, User_Type FROM LOG_IN WHERE 
(Log_User = '" . mysql_real_escape_string($_POST['username1']) . "') 
and 
(Log_Pass = '" . mysql_real_escape_string($_POST['password']) . "') 
and 
(User_Type = 'member')") 
or die('Query failed: ' . mysql_error() . "<br />\n$sql"); ; 


//Check username and password match 
if (mysql_num_rows($login) == 1) 
{ 
    if(trim('$new_pass') == trim('$con_pass')) 
    { 
     $sql=mysql_query("UPDATE log_in SET Log_Pass='$new_pass' where username='$user'"); 
     if(!$sql) 
     { 
      echo "fail updating!"; 
     } 
     else 
     { 
      echo "success!"; 
      echo "<script type = text/javascript>"; 
      echo "alert('The new password has been changed successfully.');"; 
      echo "</script>"; 
     } 
    } 
    else 
    { 
     echo "fail!"; 
     echo "<script type = text/javascript>"; 
     echo "alert('Error. New Password and Confirm Password are not the same. Please make it sure that they are the same.');"; 
     echo "</script>"; 
    } 
} 
} 
?> 

</div> 

<div class="copyright"> 
&copy; Copyright 2011 <strong>ASM Services Inc.</strong> 
</div> 
</body> 
</html> 

這是我改變用戶密碼的整個代碼。我真的不知道我的代碼有什麼確切的錯誤。每當我更改密碼時,它總是會出現「錯誤。新密碼和確認密碼不一樣」。我的php更改密碼有什麼問題?

+0

這確實是一個 「太本地化」 的問題 –

+1

你存儲在**明文密碼**;這是你的代碼中唯一*最*錯的東西。請參閱:http://stackoverflow.com/questions/401656/secure-hash-and-salt-for-php-passwords – Piskvor

+0

我不會說這是最*錯誤的事情。我個人不介意我的密碼是否以純文本格式存儲。要在一些蹩腳的網站上註冊,我會使用mailinator並且不在乎它的安全性 –

回答

3

您當前的代碼有:

if(trim('$new_pass') == trim('$con_pass')) { 
    // passwords match 
} else { 
    // passwords don't match 
} 

你是比較字符串'$new_pass' & '$con_pass'而不是變量$new_pass & $con_pass。也不要使用不應該使用trim,因爲用戶可能在他的密碼中有空間。

$new_pass = trim($_POST['new_pass']); 
$con_pass = trim($_POST['con_pass']); 

你不應該在這裏使用trim還有:

變化

if(trim('$new_pass') == trim('$con_pass')) 

if($new_pass == $con_pass) 

你也從形式讀取密碼。如果用戶想要在密碼的末尾/開始處留出空間,則用戶認爲他的密碼有空格但您在數據庫中輸入的密碼沒有空間時,邏輯將失敗。

+0

謝謝你提醒我..當我改變我的代碼,然後我得到的下一個錯誤是「失敗更新」..你認爲我的錯誤??再次感謝 –

+0

@Kevzz你的sql一定有什麼問題。嘗試通過執行var_dump(mysql_error())來查看錯誤;回聲「失敗更新」後; – Jeune

+0

@Jeune,謝謝你給我的代碼..我知道我的代碼的錯誤..和它現在的工作..:D非常感謝上帝保佑:D –

-1

變化

if(trim('$new_pass') == trim('$con_pass')) 

if(trim($new_pass) == trim($con_pass)) 
+0

-1 codaddict已經[說](http:///stackoverflow.com/questions/8075820/what-wrong-with-my-php-change-password-pls-help/8075842#8075842) – yannis