Home > Article > Backend Development > php password change page
PHP Password Change Page
We need to use passwords when performing many business operations. The issue of password confidentiality has always been a very important topic. When it comes to data security, passwords should be encrypted using the highest level of protection. But even if your password is impeccable, if you forget it, your data will become inaccessible. In this case, we need a password change tool to help us reset a new password to ensure that the data can be used in a timely manner.
In PHP, creating a password change page is relatively simple. The following are the main steps and implementation process.
Step 1 - Establish a database connection
The first step is to establish a database connection. Here we can use MySQL database. In PHP's MySQL function, we can use the mysqli_connect() function to connect to the MySQL database.
Sample code:
$dbhost = 'localhost'; //Database host name
$dbname = 'mydatabase'; //Database name
$dbuser = 'root' ; //Database username
$dbpass = ''; //Database password
$conn = mysqli_connect("$dbhost", "$dbuser", "$dbpass") or die ("Connect Error");
mysqli_select_db($conn,"$dbname") or die ("Cannot connect to the database");
If the connection fails, "Connection error" or " Cannot connect to database" and other prompt messages.
Step 2 - Create a password change form
We need to create a password change form in HTML.
Sample code:
$result = mysqli_query($conn, $sql) or die(mysqli_error ($conn));
//Old password verification successful
} else {
//Old password verification failed
}
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
echo "Password changed successfully";
} else {
echo "Password changed Failed, please try again";
}
Finally, by combining all the codes together, we can create a complete PHP password change page.
Full code:
$dbhost = 'localhost'; //Database host name
$dbname = 'mydatabase'; //Database name
$dbuser = 'root'; //Database user name
$dbpass = ''; //Database password
$conn = mysqli_connect("$dbhost", "$dbuser", "$dbpass ") or die ("Connection error");
mysqli_select_db($conn,"$dbname") or die ("Cannot connect to database");
if(isset($_POST ['submit'])) {
$oldpassword = md5($_POST['oldpassword']);
$newpassword = md5($_POST['newpassword']);
$confirmpassword = md5( $_POST['confirmpassword']);
$id = $_POST['id'];
$sql = "SELECT * FROM userdata WHERE password = '$oldpassword'";
$ result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if(mysqli_num_rows($result) > 0) {
if($newpassword == $confirmpassword) { $sql = "UPDATE userdata SET password='$newpassword' WHERE id='$id'"; $result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); if(mysqli_affected_rows($conn) > 0) { echo "密码修改成功"; } else { echo "密码修改失败,请重试"; } } else { echo "两次输入的新密码不一致,请重新输入"; }
} else {
echo "旧密码验证失败";
}
}
?>
Through the above steps, we can create a complete PHP password modification page to ensure data security.
The above is the detailed content of php password change page. For more information, please follow other related articles on the PHP Chinese website!