我正在运行一个简单的服务,用户必须登录才能操作特殊的功能.
我的MySQL数据库存储username,password和user_id.
当用户想要登录时,他们必须提供发布到profile.php的用户名和密码.
profile.php做了一个简单的检查:
// Sanity Check
if(empty($_POST['smart_email'])|| empty($_POST['smart_password']))
{
echo 'Sorry, wrong login/passwd';
exit;
}
else
{
//
$smart_email = $_POST['smart_email'];
$smart_password=$_POST['smart_password'];
// Check if registerd and password matches
if(DB_IsAuthorized($smart_email, $smart_password) == true)
{
// Obtain proper UserID from the database
$UserID = DB_GetId($smart_email);
// set the session user_id variable
$_SESSION['user_id'] = $UserID;
//
// Display the User profile page
//
}
}
Run Code Online (Sandbox Code Playgroud)
从那时起,与用户相关的每个页面都会检查user_id设置,$_SESSION以确定此用户是否已登录并已获得授权.
if (isset($_SESSION['user_id']) && is_numeric($_SESSION['user_id']) && $_SESSION['user_id']>0)
{
// USER IS LOGGED IN
}
Run Code Online (Sandbox Code Playgroud)
问题是:这个$_SESSION['user_id']检查是否足以保护未登录用户的页面?
这个问题太宽泛,但简单的答案是否定的。
首先,您需要 https 来确保通过使用防火墙和其他所需的安全工具来保护用户免受黑客攻击。
其次,您需要使用 htaccess 更改扩展名,例如显示用户 .html 而不是 .php
第三,Session很容易被黑客劫持。因此,请始终尝试存储加密的会话值而不是纯文本。
还有很多问题需要解决,但其过于复杂和广泛。