search
HomeBackend DevelopmentPHP Tutorialmysql - PHP验证登录状态的效率和安全性问题

在用PHP做验证登录时遇到了一些问题。我的验证方法是:

  1. 规定一个私钥key,加密算法是把user_id和key连接起来后求md5
  2. 把user_id和加密后的密文存到cookies中
  3. 验证时从cookies取出user_id和密文,对user_id再进行一次加密,比较两个密文

想问一下这样做能否确保安全性?md5是否可靠?还有私钥有没有必要设置成与user_id相关的随机字串?

另外,一般网站在登录后都会在每个页面(通常是右上角)显示用户昵称或者用户名,甚至用户的头像,这些数据是存放在数据库中的,如果每个页面都要根据user_id来读取数据库,会不会造成效率下降?如果会,是否要把这些信息存放到cookies或者sessions中?或者是否要对读取数据库的方法进行优化?

回复内容:

在用PHP做验证登录时遇到了一些问题。我的验证方法是:

  1. 规定一个私钥key,加密算法是把user_id和key连接起来后求md5
  2. 把user_id和加密后的密文存到cookies中
  3. 验证时从cookies取出user_id和密文,对user_id再进行一次加密,比较两个密文

想问一下这样做能否确保安全性?md5是否可靠?还有私钥有没有必要设置成与user_id相关的随机字串?

另外,一般网站在登录后都会在每个页面(通常是右上角)显示用户昵称或者用户名,甚至用户的头像,这些数据是存放在数据库中的,如果每个页面都要根据user_id来读取数据库,会不会造成效率下降?如果会,是否要把这些信息存放到cookies或者sessions中?或者是否要对读取数据库的方法进行优化?

在使用了salt string之后,md5其实足够可靠

固定的私钥来做salt其实不好,一旦私钥泄漏之后就很麻烦,而且最痛苦的其实是你不知道你到底有没有泄漏

用户的密码字符串作为salt是更好的办法

签名字符串:

<code>$sign = md5('$user_id+$user+_password[+浏览器UA[+IP地址[...]]]');
</code>

里面爱加什么你可以自己发挥

存储到cookie里面的字符串为

<code>$token = $user_id.','.$sign;
</code>

然后你需要对用户记录做一个服务器端的缓存,缓存通过用户编号查询,里面至少要包含用户的密码

每次用户请求时,都会从缓存里面去拿他的密码来重新计算数字签名做比对

这样的好处有:

  1. 不存在私钥泄漏的问题,即使出问题也不会影响所有用户
  2. slat字符串泄漏就等同于用户的密码泄漏,逻辑上是严密的
  3. 即使有人持有$token字符串,只要用户一修改密码,老的$token就马上失效了

在此基础上还可以有其它的发挥,比如用户记录设计专门的一个字段:salt,每次用户登录成功时就生成一个随机字符串更新到salt内,数字签名计算用

<code>// 记得把$user_salt也放到缓存里
$sign = md5('$user_id+$user_password+$user_salt+...');
</code>

这样一来,每次用户重新登录之后,之前的$token一定会失效,不但安全性更高,而且还实现了每次只允许一个人登录使用

最后,记得在用户修改密码之后更新你的缓存

PS:
我自己的系统除了使用用户密码来计算数字签名之外,还使用了浏览器UA加固定私钥的方式对$token做了一次对称加密才放到cookie内,算是又多一层保护

目前我这么干:

一个数组:

$cookie = array();
$cookie['username'] = '';
$cookie['nickname'] = '';
$cookie['email'] = '';
$cookie['userid'] = '';
$cookie['security_token'] = '';
$cookie = serialize($cookie);

$seCookie = AES::encode(SYSTEM_AUTH_KEY,$cookie);
Cookie::set('__token',$seCookie);
Cookie::set('__security_token',$security_token);
/**
 * cookie中还有会话id可以和security_token进行绑定验证
 */

其他页面的验证就看着办好了。此外还可绑定IP、UserAgent等参数。security_token

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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 Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.