문제

I have a page on my website that allows the users to change their passwords

The form asks for username, current password, new password, confirm new password.

If a user enters the incorrect username, the form won't change their password.

But if a user enters the wrong password, the form changes their password anyway.

My code is pasted below, if anyone can help, I would be grealt appreciative! Thanks!

Joost

Changepassword information screen:

<div id="inlogscherm">
    <form name="form1" method="post" action="changepw.php">
        <div class="textm">Change password</div><br>
        <div class="text">Username:</div><div class="invulbalkje"><? echo "{$_SESSION['myusername']}"; ?></div><br />
        <input name="username" type="hidden" id="username" value="<? echo "{$_SESSION['myusername']}"; ?>">
        <div class="text">Password:</div><input name="npassword" type="password" id="npassword" class="invulbalkje"><br />
        <div class="text">New Password:</div><input name="newpassword" type="password" id="newpassword" class="invulbalkje"><br />
        <div class="text">Repeat New Password:</div><input name="repeatnewpassword" type="password" id="repeatnewpassword" class="invulbalkje"><br />
        <input type="submit" name="Submit" value="Change" class="button">
    </form>
</div>

here is the php for the change.(changepw.php)

    <?php 
session_start();

$host="localhost";

$username=",";

$password=","; 

$db_name=","; 

$tbl_name=",";

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$username = $_POST['username'];
$password = $_POST['password'];
$newpassword = $_POST['newpassword'];
$repeatnewpassword = $_POST['repeatnewpassword'];

$encrypted_password=md5($password);
$encrypted_newpassword=md5($newpassword);

$result = mysql_query("SELECT password FROM $tbl_name WHERE username='$username' and password = '$encrypted_password'");

if(!$result) 
{ 
    header("location:error1.php"); 
} 
if(mysql_num_rows($result)){
    if($newpassword==$repeatnewpassword){
        $sql=mysql_query("UPDATE $tbl_name SET password='$encrypted_newpassword' where username='$username'");        
        if($sql) 
        { 
                header("location:success.php");
        }
        else
        {

           header("location:error3.php");
        }       
    } else {

        header("location:error_password_not_matched.php");
    }
} else {

    header("location:error.php"); 
}
?> 

If you see the problem please contact me. I will be very thankful for that!

도움이 되었습니까?

해결책

Here is one error, As I have found.

That is it, In the form, You are using the npassword name for the password field and at the time of getting it by $password = $_POST['password']; name password as you can see here.

So simply change this code:

$password = $_POST['password'];

With

$password = $_POST['npassword'];

And this will work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top