我正在使用我在其他网页上使用的代码,这在其他网页上运行正常,但在新网页上却没有。用于登录的MySQLi表也设置完全相同。我不认为这是登录脚本,因为我将来自其他站点的密码哈希插入数据库进行测试,我可以用它登录。
这是注册码:
include('sql.php');
$username = $_POST['username'];
$password = $_POST['password'];
$confirm = $_POST['confirm'];
if($username == '' || $password = '') {
header('Location:/register.php');
}
if($password != $confirm) {
header('Location:/register.php');
}
$sql = "INSERT INTO login (username, password) VALUES ('" . $username . "', '" . password_hash($password, PASSWORD_DEFAULT) . "')";
if(mysqli_query($mysqli, $sql)) {
header('Location: /dashboard.php');
} else {
echo $mysqli->error;
}
mysqli_close($mysqli);
登录:
session_start();
$error = '';
if(isset($_POST['submit'])) {
if(empty($_POST['username']) || empty($_POST['password'])) {
header("Location: /admin.php?error=invalid");
} else {
include('sql.php');
$username = mysqli_real_escape_string($mysqli, stripslashes($_POST['username']));
$password = mysqli_real_escape_string($mysqli, stripslashes($_POST['password']));
$sql = "SELECT * FROM login WHERE username='" . $username . "'";
$result = $mysqli->query($sql);
if($result->num_rows == 1) {
while($row = $result->fetch_assoc()) {
$verify = password_verify($password, $row['password']);
if($verify == false) {
header("Location: /admin.php?error=mismatch");
} else {
$_SESSION['login_user'] = $username;
$_SESSION['login_pass'] = $password;
if($_POST['stay'] == 'stay') {
setcookie('username', $username, time() + 31536000, '/');
setcookie('password', $password, time() + 31536000, '/');
}
header("location: /dashboard.php");
}
}
} else {
header("Location: /admin.php?error=mismatch");
}
mysqli_close($mysqli);
}
}