这些是实现安全PHP用户登录的正确步骤吗?


Are these the correct steps to implement secure PHP user login?

我需要直接但安全(不是诺克斯堡,而是明智的)登录我的PHP站点。我认为这样做的步骤是:

  1. 用户通过HTTPS访问网站

  2. 登录屏幕上输入的用户名和密码,在服务器上进行哈希处理,并与预先哈希的密码(从他们注册时开始)进行比较,存储在用户记录的数据库中。我打算使用 password_hash 进行哈希处理(使用盐)

  3. 所有数据库查询都使用参数绑定来防止 SQL 注入

  4. 当用户登录时,我设置$_SESSION['logged_in'] = true并使用session_regenerate_id

  5. 我设置session.use_only_cookies选项

我在这里错过了什么吗?

另外,我不确定防止CSRF,XSS攻击的最佳方法,有什么建议吗?

如果相关,我将托管在Google App Engine上,并使用他们的NoSQL云数据存储作为我的数据库,并使用PHP-GDS库(https://github.com/tomwalder/php-gds)作为我的数据库界面。

如果你要回显任何用户输入数据,你需要使用 PHP 的 htmlspecialchars() 函数来转义它,以防止 XSS 攻击。

$foo = '<script>alert("This is bad!")</script>';
// produces a browser alert!
echo $foo;
// just write down the "<script>alert("This is bad!")</script>" string,
// because it converts "<" to "&lt;" and so on...
echo htmlspecialchars( $foo );