我是一名学生,对于一个学校项目,我们必须建立一个网站,我们还有很多时间,它还没有真正开始,但我太兴奋了,因为这是我在这个领域的第一个项目,我尝试过做一个简单的登录页面并使用 php 来哈希密码,但我就是无法让它工作..idk... 我感觉html甚至没有到达php程序。 感谢任何人回复此问题 这是我的 html 和 php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
.login-container {
width: 300px;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.login-container h2 {
text-align: center;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
margin-bottom: 5px;
}
.form-group input {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}
.form-group button {
width: 100%;
padding: 10px;
background-color: #007bff;
border: none;
border-radius: 4px;
color: #fff;
cursor: pointer;
}
.form-group button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="login-container">
<h2>Login</h2>
<form id="login-form" action="process_login.php" method="post">
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
</div>
<div class="form-group">
<button type="submit">Login</button>
</div>
</form>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-sha256/0.9.0/sha256.min.js"></script>
<script>
document.getElementById('login-form').addEventListener('submit', function(event) {
event.preventDefault();
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
var hashedPassword = sha256(password);
console.log('Username:', username);
console.log('Hashed Password:', hashedPassword);
document.getElementById('username').value = '';
document.getElementById('password').value = '';
});
</script>
</body>
</html>
还有 PHP
<?php
$servername = "localhost:8889";
$username = "root";
$password = "root";
$dbname = "TestHash";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$hashedPassword = hash('sha256', $password);
$stmt = $conn->prepare("INSERT INTO user (mail, pw) VALUES (?, ?)");
$stmt->bind_param("ss", $username, $hashedPassword);
if ($stmt->execute()) {
} else {
echo "Error: " . $stmt->error;
}
$stmt->close();
} else {
echo "Error";
}
$conn->close();
我想做的是对密码进行哈希处理,然后将哈希密码放入我的数据库中
<?php
$servername = "localhost:8889";
$username = "root";
$password = "root";
$dbname = "TestHash";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$hashedPassword = hash('sha256', $password);
$stmt = $conn->prepare("INSERT INTO user (mail, pw) VALUES (?, ?)");
// $stmt->bind_param("ss", $username, $hashedPassword);
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
if ($stmt->execute()) {
} else {
echo "Error: " . $stmt->error;
}
$stmt->close();
} else {
echo "Error";
}
$conn->close();
USE password_hash INSTEAD of hash