简体   繁体   English

PHP会话和唯一用户令牌

[英]PHP Sessions and Unique User Tokens

I am trying to implement a thing like OAuth in PHP and what I want to do is give tokens to users so they can use their private resources. 我正在尝试在PHP中实现类似OAuth的功能,而我想做的就是向用户提供令牌,以便他们可以使用其私有资源。 Every user first must login with their email and password and get a unique token which would be valid forever unless it's idle for "n" minutes. 每个用户首​​先必须使用他们的电子邮件和密码登录并获得唯一的令牌,该令牌将永远有效,除非它空闲了“ n”分钟。 So if there are zero requests for "n" minutes, the token should be destroyed. 因此,如果在“ n”分钟内的请求为零,则令牌应被销毁。 The token would be used to access private resources by users. 令牌将被用户用来访问私有资源。

One thing I can think of doing this is as... 我可以想到的一件事是...

I would maintain a db table of named user_tokens and as they login with their username and password the entry with unique token would be created there. 我将维护一个名为user_tokens的数据库表,当他们使用其用户名和密码登录时,将在此处创建具有唯一令牌的条目。 The last accessed timestamp would be set and user would given the unique token as response. 将设置最后一次访问的时间戳,并且用户将给出唯一令牌作为响应。 The token now can be used to access private resources of user and would be required to pass with all request requiring token. 现在,令牌可以用于访问用户的私有资源,并且需要与所有需要令牌的请求一起传递。 Every private request would check if the last timestamp and current timestamp has "n" minutes of difference, if yes, destroy token. 每个私有请求都将检查最后一个时间戳和当前时间戳是否相差“ n”分钟,如果是,则销毁令牌。 Otherwise send response with requested resources and set last timestamp to current timestamp. 否则,发送带有请求资源的响应,并将上一个时间戳设置为当前时间戳。

Does that make sense? 那有意义吗? Or there can be another efficient way of doing this? 还是可以有另一种有效的方法来做到这一点?

I would like to add that the token must be like what twitter or facebook returns out of their API. 我想补充一点,令牌必须类似于twitter或facebook从其API返回的内容。

I'm using session_regenerate_id to generate the token, before I was using session_id but I had figure out that using session_id the sessID was never change the value, then regenerating it was a solution that fits to me. 在使用session_regenerate_id之前,我正在使用session_id生成令牌,但我发现使用session_id不会使sessID更改值,然后重新生成它是适合我的解决方案。

Now I can even control if there are the same user but with the different sessID and give the option to LogOut the sessions of one user. 现在,我什至可以控制是否有相同的用户,但拥有不同的sessID,并提供注销一个用户的会话的选项。

I do not know if it is exactly what you are looking for, but it solve my problem about TOKEN. 我不知道这是否正是您要寻找的东西,但是它解决了我关于TOKEN的问题。 And it will never repeat. 而且它永远不会重复。

Reference: https://www.php.net/manual/en/function.session-regenerate-id.php 参考: https : //www.php.net/manual/zh/function.session-regenerate-id.php

If you're looking to implement OAuth using a library you should check out the HTTP_OAuth pear package by Jeff Hodson of Digg[1], and there are a bunch of good posts on this site about database design to use with Oauth[2]. 如果您希望使用库来实现OAuth,则应查看Digg [1]的Jeff Hodson的HTTP_OAuth pear软件包,该站点上有很多关于OAuth [2]的数据库设计的好文章。

I think I'm confused about your question, though. 不过,我想我对您的问题感到困惑。 Are you looking to make an API for your web application, or just provide a way to protect a user's resources? 您是要为Web应用程序创建API,还是只是提供一种保护用户资源的方法? If you want to make an API, you should definitely use OAuth and also use a well known library. 如果要创建API,则一定要使用OAuth并使用众所周知的库。 Doing so will ensure that: 这样做将确保:

  1. Other developers will know how to use your API because it follows the OAuth RFC[3] 其他开发人员将知道如何使用您的API,因为它遵循OAuth RFC [3]
  2. Your web application is much more likely to be secure 您的Web应用程序更可能是安全的
  3. You become aware of best practices and learn some new stuff 您会了解最佳做法并学习一些新知识

If you aren't looking to make an API, and just want to protect user resources, I think you would be safe using sessions[4], and, if the user is not logged in they can't access the protected resources. 如果您不打算制作API,而只是想保护用户资源,我认为使用session [4]是安全的,并且,如果用户未登录,他们将无法访问受保护的资源。

[1] HTTP_OAuth Package : http://pear.php.net/pepr/pepr-proposal-show.php?id=607 [1] HTTP_OAuth软件包: http ://pear.php.net/pepr/pepr-proposal-show.php?id=607
[2] Oauth Database Design : what is the recommended database structure for OAuth Provider [2] Oauth数据库设计: OAuth Provider推荐的数据库结构是什么
[3] Oauth RFC : http://oauth.net/ [3] Oauth RFC: http://oauth.net/http://oauth.net/
[4] PHP Sessions : http://us2.php.net/manual/en/features.sessions.php [4] PHP会话: http://us2.php.net/manual/en/features.sessions.phphttp://us2.php.net/manual/en/features.sessions.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM