我为我的 web 项目构建了一个基本的 php 身份验证系统。我只想问它是否安全,因为我只是担心会话劫持和 sql 注入问题。代码如下。
用户表单字段包含用于电子邮件的 user_email 字段名称和用于密码的密码字段名称
PHP user validation code
<?php
session_start();
// // check if user session is set or not
if(isset($_SESSION['user'])){
// session is set redirect to user home
header('Location: appointments.php');
}
// // checking if request method is post
if( $_SERVER['REQUEST_METHOD'] === "POST" ){
if(isset($_POST['user_email']) && isset($_POST['password']) ){
// including database file for database connection
include 'database_connection.php';
$stmt = $conn->prepare("SELECT * FROM user WHERE email = ? AND password = ?");
$stmt->execute([ $_POST['user_email'] , $_POST['password'] ]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if( $stmt->rowCount() > 0 ){
$_SESSION['user'] = $result['first_name'];
$_SESSION['user_first_name'] = $result['first_name'];
$_SESSION['user_last_name'] = $result['last_name'];
$_SESSION['user_email'] = $result['email'];
$_SESSION['user_contact'] = $result['contact'];
header('Location: user_appoinment_application.php');
die();
}
else{
header('Location: appointments.php');
die();
}
}
else{
header('Location: appointments.php');
die();
}
}
// request method get
else{
header('Location: appointments.php');
}
用于检查特定页面的用户授权我将以下用于检查用户是否登录的代码放在页面顶部
session_start();
// checking the user is logged in or not
if(!isset($_SESSION['user_first_name'])){
// session is set redirect to doctor home
header('Location: appointments.php');
}
我知道防止 sql 注入攻击使用 sql 准备好的语句,但我不知道如何防止会话劫持。现在我只想知道上面的代码是否安全。提前致谢