我正在运行一项简单的服务,用户必须登录才能操作特殊的功能。
我的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
//
}
}
从那时起,与用户相关的每个页面都会检查user_id
中设置的$_SESSION
,以确定此用户是否已登录并已获得授权。
if (isset($_SESSION['user_id']) && is_numeric($_SESSION['user_id']) && $_SESSION['user_id']>0)
{
// USER IS LOGGED IN
}
问题是:这个$_SESSION['user_id']
是否足以检查来自未登录用户的页面?
答案 0 :(得分:3)
这个问题过于宽泛,但简单的答案是不。
首先,您需要使用https来确保使用防火墙和其他必需的安全工具来保护用户免受黑客攻击。
其次,您需要使用htaccess来更改扩展名,例如show user .html而不是.php
第三,黑客可以轻易地劫持会话。因此,请始终尝试存储加密的会话值而不是纯文本。
还有很多问题要处理,但它过于复杂和广泛。