我为我的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准备语句,但我不知道如何防止会话劫持。现在我只想知道上面的代码是否安全。提前致谢
答案 0 :(得分:3)
为了使其免受会话劫持的影响,您需要了解一些事项。
会话端劫持
这是在网络上使用数据包嗅探器来监控网络活动的地方,我们可以专注于双方之间的通信,并希望以这种方式窃取会话cookie。这可以通过在网站上的任何位置启用SSL来避免。有些人只在网站的身份验证部分使用SSL。这不够好,它需要在网站的任何地方。
会话固定
当网站接受URL中的SID或POST数据时,就会发生这种情况。恶意用户可以通过向URL中具有其选择的SID的受害者发送电子邮件来设置会话ID。即http://example.com/?SID=I_WILL_GET_YOUR_ID。现在,恶意用户只是等待受害者点击发送给他/她的链接,一旦受害者登录恶意用户就可以使用上述URL来劫持会话。
跨网站脚本
恶意用户欺骗受害者运行似乎属于服务器的代码,因此允许恶意用户编写特定代码来窃取会话cookie。
<强>结论强>
对于在整个站点上使用SSL的一部分,将阻止会话端劫持。您必须谨慎的另一部分是XSS漏洞利用。我建议查看https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet 因为它在编写客户端代码时有一个很好的检查清单。
我希望这会有所帮助。