PHP Password Hashing
PHP password hashing ensures secure storage of user passwords. It uses password_hash()
to encrypt and password_verify()
to validate the password.
PHP password hashing पासवर्ड को सुरक्षित रूप से स्टोर करने के लिए उपयोग किया जाता है। इसके लिए password_hash()
और password_verify()
फंक्शन का प्रयोग होता है।
Register with Password Hashing
<?php
$conn = new mysqli("localhost", "root", "", "test");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
$hashed = password_hash($password, PASSWORD_DEFAULT);
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
$stmt->bind_param("ss", $username, $hashed);
$stmt->execute();
echo "Registered Successfully ✅";
}
?>
<form method="POST">
Username: <input type="text" name="username" required><br>
Password: <input type="password" name="password" required><br>
<input type="submit" value="Register">
</form>
Login with password_verify()
<?php
$conn = new mysqli("localhost", "root", "", "test");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
$stmt = $conn->prepare("SELECT password FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$row = $result->fetch_assoc();
if (password_verify($password, $row['password'])) {
echo "Login Successful ✅";
} else {
echo "Wrong Password ❌";
}
} else {
echo "User Not Found ❌";
}
}
?>
<form method="POST">
Username: <input type="text" name="username" required><br>
Password: <input type="password" name="password" required><br>
<input type="submit" value="Login">
</form>
users Table SQL
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) NOT NULL,
password VARCHAR(255) NOT NULL
);
Output Example:
1. Register with username
2. Login with same details →
3. Login with wrong password →
1. Register with username
admin
and password 12345
2. Login with same details →
Login Successful ✅
3. Login with wrong password →
Wrong Password ❌