PHP是$_SESSION足以保护网页?

前端之家收集整理的这篇文章主要介绍了PHP是$_SESSION足以保护网页?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在运行一个简单的服务,用户必须登录才能操作特殊的功能.

我的MySQL数据库存储用户名,密码和user_id.

用户想要登录时,他们必须提供发布到profile.PHP用户名和密码.

profile.PHP做了一个简单的检查:

  1. // Sanity Check
  2. if(empty($_POST['smart_email'])|| empty($_POST['smart_password']))
  3. {
  4. echo 'Sorry,wrong login/passwd';
  5. exit;
  6. }
  7. else
  8. {
  9. //
  10. $smart_email = $_POST['smart_email'];
  11. $smart_password=$_POST['smart_password'];
  12. // Check if registerd and password matches
  13. if(DB_IsAuthorized($smart_email,$smart_password) == true)
  14. {
  15. // Obtain proper UserID from the database
  16. $UserID = DB_GetId($smart_email);
  17. // set the session user_id variable
  18. $_SESSION['user_id'] = $UserID;
  19. //
  20. // Display the User profile page
  21. //
  22. }
  23. }

从那时起,与用户相关的每个页面都会检查$_SESSION中设置的user_id,以确定此用户是否已登录并已获得授权.

  1. if (isset($_SESSION['user_id']) && is_numeric($_SESSION['user_id']) && $_SESSION['user_id']>0)
  2. {
  3. // USER IS LOGGED IN
  4. }

问题是:这个$_SESSION [‘user_id’]是否足以检查来自非登录用户页面

最佳答案
这个问题过于宽泛,但简单的答案是否定的.

首先,您需要使用https来确保通过使用防火墙和其他必需的安全工具来保护用户免受黑客攻击.

其次,您需要使用htaccess来更改扩展名,例如show user .html而不是.PHP

第三,黑客可以轻易地劫持会话.因此,请始终尝试存储加密的会话值而不是纯文本.

还有很多问题要处理,但它过于复杂和广泛.

猜你在找的MySQL相关文章