PHP“Session_regenerate_id”和用户身份验证 [英] PHP "Session_regenerate_id" and Authentication of users
问题描述
我在我的网站上创建了一个登录功能,并且我正在考虑在每个页面上重新生成会话ID,以使其更安全。
PHP:有关regenerate_id的信息,但PHP页面上的帖子与他们提供的有关session_regenerate_id的信息完全不同。有人可以解释这两个问题:
-
我是否需要将旧会话数据复制到新生成的数据中,还是自动完成?代码示例非常感谢......
-
如何检查用户是否已经登录?我应该在会话变量中存储什么,以及如何?代码示例非常感谢...
谢谢
当您调用session_regenerate_id()时,不需要复制会话数据。这一切都是由PHP为您照顾的。基本上会创建一个新的会话标记和cookie,将会话数据复制到会话存储中以与新标记相关联,并且如果将true作为单个参数传递给函数,则会删除磁盘上的旧会话数据文件。 p>
您在会话中存储的指示用户是否登录的内容取决于您。我经常只是存储一个简单的布尔值来表示它们是否已经登录,以及其他值包含用户名,名称等。然后检查是否有人登录,就像这样简单:
<?php
if($ _SESSION ['logged_in']){
//用户登录
} else {
//用户未登录
}
?>
HTH。
I am creating a login-function on my website, and I am thinking about regenerating the session ID on every page to make things more secure.
I have read PHP:s information about regenerate_id but the posts on the PHP page are quite different from the information they provide about session_regenerate_id.
Could somebody explain these two questions:
Do I need to copy the old session data into the newly generated one, or is this done automatically? Code examples are very much appreciated...
How do I check to see if a user is already logged in? What should I store in the session variable, and how? Code examples are very much appreciated...
Thanks
Calling session_regenerate_id() on every page may be a little bit of overkill, depending on your setup. The function is used to prevent session hijacking and should be used whenever a user elevates their level of privilege (such as logging in). Usually you would switch to a https connection once a user is logged in, meaning you only need to call session_regenerate_id() once as the new cookie would be tranmitted over a secure connection and wouldn't be able to be eavesdropped. However, if you don't have a SSL certificate on your server regenerating the session cookie on every page could be a good option.
When you call session_regenerate_id() you don't need to copy session data. This is all taken care of for you by PHP. Basically a new session token and cookie are created, session data is copied in the session store to be associated with the new token, and if you pass true as the single argument to the function the old session data file on disk is deleted.
What you store in the session to indicate if a user is logged in is up to you. I often just store a simple boolean value to indicate if they're logged in, along with other values holding usernames, name, etc. Then checking if someone is logged in is as simple as this:
<?php
if ($_SESSION['logged_in']){
//User logged in
} else {
//User not logged in
}
?>
HTH.
这篇关于PHP“Session_regenerate_id”和用户身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
相关文章
- Yii 错误:session_regenerate_id();
- PHP session_regenerate_id和Blackberry浏览器;
- session_regenerate_id() vs session_id(randomString);
- session_create_id 和 session_regenerate_id() 有什么区别?;
- session_id、session_regenerate_id 和 session_name 有什么用?;
- session_regenerate_id 没有创建新的会话 ID;
- session_regenerate_id()在IE11/Edge中不起作用;
- session_regenerate_id 不能与 sesion_destroy 一起使用;
- 什么时候以及为什么我应该使用session_regenerate_id()?;
- 我什么时候以及为什么应该使用 session_regenerate_id()?;
- session_regenerate_id(true) 对 ajax 请求或快速刷新的无效会话;
- Postgress 和 PHP “用户身份验证失败";
- session_regenerate_id() - 单元测试 Yii 控制器中已经发送的标头;
- Windows身份验证的用户ID和密码;
- php - 通过session_id验证身份的问题;
- 用户身份验证和登录;
- 用户存在和身份验证;
- 身份验证和用户任务;
- 在Laravel中获取身份验证用户ID;
- 用于用户身份验证的PHP会话;
- 用户身份验证和仅应用身份验证之间的区别?;
- Windows身份验证和本地数据库用户身份验证;
- Windows 身份验证和本地数据库用户身份验证;
- 获取Windows用户ID使用窗体身份验证时;
- 没有会话的PHP用户身份验证教程;
PHP最新文章
- 请求头字段Access-Control-Allow-Headers在预检响应中不允许Access-Control-Allow-Headers;
- 路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误;
- 什么是NCFB和NOFB模式?;
- 警告:mysqli_connect():(HY000 / 1045):访问被拒绝用户'用户名'@'localhost'(使用密码:是);
- 如何处理致命错误:cURL错误7:无法连接到xxxx端口443;
- 参数3传递给GuzzleHttp\Client :: request()必须是数组类型,给定字符串;
- phpMyAdmin的#2054无法登录到MySQL服务器;
- SSL错误SSL3_GET_SERVER_CERTIFICATE:证书验证失败;
- 在PHPExcel中设置字体颜色,字体和字体大小;
- 如何解决cURL错误(7):无法连接到主机?;