Account Deletion in PHP
Let's now implement the ability for a user to delete their account. Create a separate PHP page for this. When visiting it, the user should see a form where they need to enter their password. The account should only be deleted after the correct password has been entered.
The point is that account deletion is an important operation, and all operations of this kind require requesting the password to ensure that it is not a malicious actor who has gained access to the user's computer.
I will show a key piece of code:
<?php
$id = $_SESSION['id'];
$query = "SELECT * FROM users WHERE id='$id'";
$res = mysqli_query($link, $query);
$user = mysqli_fetch_assoc($res);
$hash = $user['password']; // salted password from DB
// Check the correspondence of the hash from the database to the entered old password
if (password_verify($_POST['password'], $hash)) {
$query = "DELETE FROM users WHERE id='$id'";
mysqli_query($link, $query);
} else {
// password entered incorrectly, display a message
}
?>
Implement account deletion.