SQL concepts used: SELECT · UPDATE · WHERE
For an authenticated password-change form, verify the member's existing password, validate the new password, store a fresh hash, and regenerate the session ID. The form should also include CSRF protection.
<?php
session_start();
$memberId = $_SESSION['member_id'] ?? 0;
$currentPassword = $_POST['current_password'] ?? '';
$newPassword = $_POST['new_password'] ?? '';
$stmt = $pdo->prepare(
'SELECT password_hash FROM members WHERE id = :id'
);
$stmt->execute([':id' => $memberId]);
$storedHash = $stmt->fetchColumn();
if (!$storedHash || !password_verify($currentPassword, $storedHash)) {
exit('Current password is not correct.');
}
if (strlen($newPassword) < 12) {
exit('Choose a longer password.');
}
$newHash = password_hash($newPassword, PASSWORD_DEFAULT);
$stmt = $pdo->prepare(
'UPDATE members SET password_hash = :hash WHERE id = :id'
);
$stmt->execute([':hash' => $newHash, ':id' => $memberId]);
session_regenerate_id(true);
?>

// check the login details of the user and stop execution if not logged in
require "check.php";
Inside the file check.php we have only few lines of code to check the session. Here it is for your reference.
<?Php
if(!isset($_SESSION['userid'])){
echo "<center><font face='Verdana' size='2' color=red>
Sorry, Please login and use this page </font></center>";
exit;
}
?>
Now let us collect all the form posted data of the user
$todo=$_POST['todo'];
$password=$_POST['password'];
$password2=$_POST['password2'];
$old_password=$_POST['old_password'];
Now we will set the flags for validation of the variables. Please note that we have used limited validation here and you can go for more checking as per your requirements. ( like allowing only numbers or chars in the password etc. )
$status = "OK";
$msg="";
Load the stored hash with a prepared query, verify the current password, validate the new password, then store a fresh hash. Do not compare password hashes with md5() or place the session user ID directly inside the SQL string.
<?php
$currentPassword = (string) ($_POST['old_password'] ?? '');
$newPassword = (string) ($_POST['password'] ?? '');
$confirmPassword = (string) ($_POST['password2'] ?? '');
$userId = $_SESSION['userid'] ?? null;
$stmt = $dbo->prepare('SELECT password FROM plus_signup WHERE userid = :userid');
$stmt->execute(['userid' => $userId]);
$storedHash = $stmt->fetchColumn();
if (!is_string($storedHash) || !password_verify($currentPassword, $storedHash)) {
throw new RuntimeException('Current password is incorrect.');
}
if ($newPassword !== $confirmPassword || strlen($newPassword) < 12) {
throw new InvalidArgumentException('Choose a matching password of at least 12 characters.');
}
$newHash = password_hash($newPassword, PASSWORD_DEFAULT);
$update = $dbo->prepare('UPDATE plus_signup SET password = :password WHERE userid = :userid');
$update->execute(['password' => $newHash, 'userid' => $userId]);
?>
After changing the password, invalidate other authenticated sessions if your application supports session management across devices.
If the database updating is successful then the user has to use new password for next time login. Or the user can be redirected to logout page and can be asked to login again.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| flora | 15-11-2011 |
| how about adding javascript in changing password? can u pls help me.. thanks in advance :) | |
| Nitin patil | 06-09-2012 |
| Thanks u really help me. | |
| John Akuse | 23-09-2013 |
| Process for changing password interesting | |
| Prasad | 29-01-2014 |
| Thanks... it realy works...! | |
| Jue | 22-09-2014 |
| why error at config.php ? Error!: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) | |
| smo | 24-09-2014 |
| Enter correct login details for MySQL. userid , password and database name | |
| nis | 24-09-2014 |
| Error!: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) There is no databases in that code file | |