我为我的网站制作了一个登录脚本,需要一些关于它是否安全以及如何改进它的反馈:
<?php
session_start();
$user = "root";
$host = "localhost";
$pass = "";
$db = "test_db";
$cxn = mysqli_connect($host, $user, $pass, $db) or die ("Couldn't connect to the server. Please try again.");
if(isset($_POST['submit'])) {
$username = mysql_real_escape_string(strip_tags(trim($_POST['username'])));
$password = mysql_real_escape_string(strip_tags(trim($_POST['password'])));
$message = "";
$stmt = $cxn->prepare('SELECT * FROM users WHERE username = ?');
$stmt->bind_param('s', $username);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
if(password_verify($password, $row['password'])) {
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
header("location:index.php");
} else {
$message = "The username or password is incorrect.";
}
}
}
?>
另外,我只是在学习会话并且有几个问题:
- 用户成功登录后,我需要他们的用户名显示在任何其他页面上。如何使会话安全?
- 如何使“注销”功能结束会话?