Solved

How to Compare new password to existing password to prevent using the same one.

Posted on 2008-06-11
2
178 Views
Last Modified: 2010-04-06
I have a page where users are redirected to change their password.  The logic I would like to add is to prevent them from re-using their existing password.  The existing password is stored in recordset  
rsLoginData and the field name is Password.  The field name on form1 for their new password is NewPassword.

Input appreciated.
0
Comment
Question by:drelinger
2 Comments
 
LVL 6

Accepted Solution

by:
ysfx earned 250 total points
ID: 21763697
You can have another field which requests the old password, thus requiring that the user entering the new password knows the old password. This way you can verify the old password with the new password immediately.

Otherwise, you will have to pull the information from  your database to validate the the password is not the same, or you can modify the update statement's criteria (ie where) by adding passwordcolumn <> 'newpassword'.
0
 
LVL 2

Assisted Solution

by:kszurek
kszurek earned 250 total points
ID: 21820881

<?PHP
if (strlen($_POST['old']) > 0 && strlen($_POST['new']) > 0)
{
	$old = trim($_POST['old']);
	$new = trim($_POST['new']);
	mysql_connect('localhost', 'user', 'password');
	mysql_select_db('expert');
	$result = mysql_query("SELECT password FROM rsLoginData WHERE user = 'username' LIMIT 1");
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	if ($row['password'] == $old)
	{
		if ($new == $_POST['new2'])
		{
			mysql_query("UPDATE rsLoginData SET password = '".mysql_real_escape_string($new)."' WHERE user = 'username' LIMIT 1");
			echo 'Password changed';
		} else echo 'Password mishmash';
	} else echo 'Old password is incorrect';
 
}
?>
<form method="post" action="a.php">
Old: <input type="password" name="old"><br />
New: <input type="password" name="new"><br />
Retype new: <input type="password" name="new2"><br />
<input type="submit" name="submit" value="Change!">
</form>

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
alert(innerHTML); 8 32
I thought I had multiple sliders on one page working properly . . . I don't. 10 16
Button function on table is in trouble 3 20
Javascript 2 22
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question