Table of Contents
回复内容:
Home Backend Development PHP Tutorial 如何存储token比较合适?

如何存储token比较合适?

Jun 06, 2016 pm 08:30 PM
java php web rear end

在保持多步骤流程一致性时我需要用到token(比如找回密码):

步骤1,用户提交要找回密码的用户名和验证码,我生成token1,返回页面中包含这个token1

步骤2,用户提交他收到的短信验证码和token1,我根据token1来识别是哪个用户,短信验证码是否正确,token是否过期,这个token的使用场景是否正确,如果都有效,我生成一个token2,返回页面包含这个token2

步骤3,用户提交他的新密码和token2,我根据token2来找出是哪个用户,最后重置那个用户的密码

这里的token都有时效性,我存在mysql里,每天都要跑cron job来删除过期的token,这样不太好吧,我应该怎么存token?或者有更好的办法?

回复内容:

在保持多步骤流程一致性时我需要用到token(比如找回密码):

步骤1,用户提交要找回密码的用户名和验证码,我生成token1,返回页面中包含这个token1

步骤2,用户提交他收到的短信验证码和token1,我根据token1来识别是哪个用户,短信验证码是否正确,token是否过期,这个token的使用场景是否正确,如果都有效,我生成一个token2,返回页面包含这个token2

步骤3,用户提交他的新密码和token2,我根据token2来找出是哪个用户,最后重置那个用户的密码

这里的token都有时效性,我存在mysql里,每天都要跑cron job来删除过期的token,这样不太好吧,我应该怎么存token?或者有更好的办法?

token可以使用 md5(username+userid+timestamp)_timestamp,这种格式的token基本上不会重复了,同时尾部的timestample也能用于过期检测。
对于一个用户来说通常一个token就够了,当你生成第二个token的时候第一个已经失效了,所以可以直接update掉,这样每个用户最多有一个token就不需要考虑删除的问题了,当然在token被验证有效地时候应该把数据库中的token set null,防止同一个token被二次验证。

token不需要存储的,你可以使用AES的可逆向加/解密算法对数据进行加密,如aes.ecode(userId+time+info+key(密钥)),生成token后把这个串传回给客户端,不需要保存,等下次他传回给你的时候你只需要aes.decode(token)解密一下就可以获得这里面的用户信息与当前登陆的用户信息对比一下就行了

或者不用储存token,有效性信息就放在token里,你看看这个json web token

token不用存mysql吧,存内存就好了。
一个线程轮训删除过期的token数据。
当然你会说宕机了,内存数据就没了,但是基于找回密码的操作不常有,这个数据不会太多,而时效性一般不会太长,如果真遇到宕机,宕机了用户也无法访问了啊,故这个token就废弃也无所谓,让用户重新提交找回操作。

可以存memcahe或redis里,缓存设定时间过期。

我一般直接存session

token 我是用md5 用户名加 现有密码生成....
然后记录下时间

存redis,设置过期时间是最方便的

token类似于单个web工程中使用的session,保存起来便于查看访问日志,不保存也可以,但每个模块和功能的操作日志最好记录一下,以便后期运营和维护使用。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

What does 'platform independence' mean in the context of Java? What does 'platform independence' mean in the context of Java? Apr 23, 2025 am 12:05 AM

Java's platform independence means that the code written can run on any platform with JVM installed without modification. 1) Java source code is compiled into bytecode, 2) Bytecode is interpreted and executed by the JVM, 3) The JVM provides memory management and garbage collection functions to ensure that the program runs on different operating systems.

H5: Key Improvements in HTML5 H5: Key Improvements in HTML5 Apr 28, 2025 am 12:26 AM

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Composer: The Package Manager for PHP Developers Composer: The Package Manager for PHP Developers May 02, 2025 am 12:23 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

Discuss situations where writing platform-specific code in Java might be necessary. Discuss situations where writing platform-specific code in Java might be necessary. Apr 25, 2025 am 12:22 AM

Reasons for writing platform-specific code in Java include access to specific operating system features, interacting with specific hardware, and optimizing performance. 1) Use JNA or JNI to access the Windows registry; 2) Interact with Linux-specific hardware drivers through JNI; 3) Use Metal to optimize gaming performance on macOS through JNI. Nevertheless, writing platform-specific code can affect the portability of the code, increase complexity, and potentially pose performance overhead and security risks.

See all articles