


A few things about cookie information security for user login, cookie information security_PHP tutorial
One or two things about cookie information security when users log in, cookie information security
Everyone knows that after a user logs in, the user information will generally be saved in cookies, because cookies are used to save customers End,
Moreover, cookies can be freely changed on the client side using the browser. This will cause the risk of forgery of user cookies, which may allow forgers to log in to any user's account.
Here are some common methods to prevent users from logging in cookie information:
1. Cookie information encryption method
The cookie information encryption method uses an encryption method to encrypt user information and then stores the cookie. In this way, even if the forger obtains the cookie, it can only use the cookie within the validity period of the cookie and cannot forge additional cookie information.
Here is an encryption function:
<!--?<span>php </span><span>function</span> authcode(<span>$string</span>, <span>$operation</span> = 'DECODE', <span>$key</span> = '', <span>$expiry</span> = 0<span>) { </span><span>//</span><span> 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙 </span> <span>$ckey_length</span> = 4<span>; </span><span>//</span><span> 密匙 </span> <span>$key</span> = <span>md5</span>(<span>$key</span> ? <span>$key</span> : <span>$GLOBALS</span>['discuz_auth_key'<span>]); </span><span>//</span><span> 密匙a会参与加解密 </span> <span>$keya</span> = <span>md5</span>(<span>substr</span>(<span>$key</span>, 0, 16<span>)); </span><span>//</span><span> 密匙b会用来做数据完整性验证 </span> <span>$keyb</span> = <span>md5</span>(<span>substr</span>(<span>$key</span>, 16, 16<span>)); </span><span>//</span><span> 密匙c用于变化生成的密文 </span> <span>$keyc</span> = <span>$ckey_length</span> ? (<span>$operation</span> == 'DECODE' ? <span>substr</span>(<span>$string</span>, 0, <span>$ckey_length</span>): <span>substr</span>(<span>md5</span>(<span>microtime</span>()), -<span>$ckey_length</span>)) : ''<span>; </span><span>//</span><span> 参与运算的密匙 </span> <span>$cryptkey</span> = <span>$keya</span>.<span>md5</span>(<span>$keya</span>.<span>$keyc</span><span>); </span><span>$key_length</span> = <span>strlen</span>(<span>$cryptkey</span><span>); </span><span>//</span><span> 明文,前10位用来保存时间戳,解密时验证数据有效性,10到26位用来保存$keyb(密匙b), //解密时会通过这个密匙验证数据完整性 // 如果是解码的话,会从第$ckey_length位开始,因为密文前$ckey_length位保存 动态密匙,以保证解密正确 </span> <span>$string</span> = <span>$operation</span> == 'DECODE' ? <span>base64_decode</span>(<span>substr</span>(<span>$string</span>, <span>$ckey_length</span>)) : <span>sprintf</span>('%010d', <span>$expiry</span> ? <span>$expiry</span> + <span>time</span>() : 0).<span>substr</span>(<span>md5</span>(<span>$string</span>.<span>$keyb</span>), 0, 16).<span>$string</span><span>; </span><span>$string_length</span> = <span>strlen</span>(<span>$string</span><span>); </span><span>$result</span> = ''<span>; </span><span>$box</span> = <span>range</span>(0, 255<span>); </span><span>$rndkey</span> = <span>array</span><span>(); </span><span>//</span><span> 产生密匙簿 </span> <span>for</span>(<span>$i</span> = 0; <span>$i</span> <= 255; <span>$i</span>++<span>) { </span><span>$rndkey</span>[<span>$i</span>] = <span>ord</span>(<span>$cryptkey</span>[<span>$i</span> % <span>$key_length</span><span>]); } </span><span>//</span><span> 用固定的算法,打乱密匙簿,增加随机性,好像很复杂,实际上对并不会增加密文的强度 </span> <span>for</span>(<span>$j</span> = <span>$i</span> = 0; <span>$i</span> < 256; <span>$i</span>++<span>) { </span><span>$j</span> = (<span>$j</span> + <span>$box</span>[<span>$i</span>] + <span>$rndkey</span>[<span>$i</span>]) % 256<span>; </span><span>$tmp</span> = <span>$box</span>[<span>$i</span><span>]; </span><span>$box</span>[<span>$i</span>] = <span>$box</span>[<span>$j</span><span>]; </span><span>$box</span>[<span>$j</span>] = <span>$tmp</span><span>; } </span><span>//</span><span> 核心加解密部分 </span> <span>for</span>(<span>$a</span> = <span>$j</span> = <span>$i</span> = 0; <span>$i</span> < <span>$string_length</span>; <span>$i</span>++<span>) { </span><span>$a</span> = (<span>$a</span> + 1) % 256<span>; </span><span>$j</span> = (<span>$j</span> + <span>$box</span>[<span>$a</span>]) % 256<span>; </span><span>$tmp</span> = <span>$box</span>[<span>$a</span><span>]; </span><span>$box</span>[<span>$a</span>] = <span>$box</span>[<span>$j</span><span>]; </span><span>$box</span>[<span>$j</span>] = <span>$tmp</span><span>; </span><span>//</span><span> 从密匙簿得出密匙进行异或,再转成字符 </span> <span>$result</span> .= <span>chr</span>(<span>ord</span>(<span>$string</span>[<span>$i</span>]) ^ (<span>$box</span>[(<span>$box</span>[<span>$a</span>] + <span>$box</span>[<span>$j</span>]) % 256<span>])); } </span><span>if</span>(<span>$operation</span> == 'DECODE'<span>) { </span><span>//</span><span> 验证数据有效性,请看未加密明文的格式 </span> <span>if</span>((<span>substr</span>(<span>$result</span>, 0, 10) == 0 || <span>substr</span>(<span>$result</span>, 0, 10) - <span>time</span>() --> 0) && <span>substr</span>(<span>$result</span>, 10, 16) == <span>substr</span>(<span>md5</span>(<span>substr</span>(<span>$result</span>, 26).<span>$keyb</span>), 0, 16<span>)) { </span><span>return</span> <span>substr</span>(<span>$result</span>, 26<span>); } </span><span>else</span><span> { </span><span>return</span> ''<span>; } } </span><span>else</span><span> { </span><span>//</span><span> 把动态密匙保存在密文里,这也是为什么同样的明文,生产不同密文后能解密的原因 // 因为加密后的密文可能是一些特殊字符,复制过程可能会丢失,所以用base64编码 </span> <span>return</span> <span>$keyc</span>.<span>str_replace</span>('=', '', <span>base64_encode</span>(<span>$result</span><span>)); } } </span><span>$str</span> = 'abcdef'<span>; </span><span>$key</span> = 'www.phpskill.com'<span>; </span><span>echo</span> <span>$jm</span> = authcode(<span>$str</span>,'ENCODE',<span>$key</span>,0); <span>//</span><span>加密 </span> <span>echo</span> " "<span>; </span><span>echo</span> authcode(<span>$jm</span> ,'DECODE',<span>$key</span>,0); <span>//</span><span>解密</span> ?>
This way when the cookie for user information is set, it cannot be forged:
<!--?<span>php </span><span>$user</span> = <span>array</span>("uid"=--><span>$uid</span>,"username"=><span>$username</span><span>); </span><span>$user</span> = <span>base64_encode</span>(<span>serialize</span>(<span>$user</span><span>)); </span><span>$user</span> = authcode(<span>$user</span>,'ENCODE','www.phpskill.com',0); <span>//</span><span>加密 </span> <span>setcookie</span>("user",<span>$user</span>,<span>time</span>()+3600*24<span>); </span>?>
2. Protect cookies with encryption tokens
<span>$hash</span> = <span>md5</span>(<span>$uid</span>.<span>time</span>());<span>//</span><span>加密令牌值</span> <span>$hash_expire</span> =<span>time</span>()+3600*24;<span>//</span><span>加密令牌值为一天有效期</span> <span>$user</span> = <span>array</span>("uid"=><span>$uid</span>,"username"=><span>$username</span>,"hash"=><span>$hash</span><span>); </span><span>$user</span> = <span>base64_encode</span>(<span>serialize</span>(<span>$user</span><span>)); </span><span>setcookie</span>("user",<span>$user</span>,<span>$hash_expr</span><span>); 然后把</span><span>$hash和$hash_expire</span> 存入member表中hash和hash_expire对应字段中,<span>也可以存入nosql,session 用户伪造cookie时,hash无法伪造</span>,<span>伪造的hash和数据库中的不一致 用户每次登陆,这个hash_expire有效期内不更新hash值,过期则更新</span>
php pure technical exchange group: 323899029
Original text reprinted at: http://www.phpskill.com/html/show-1-4424-1.html

In PHP, you can use session_status() or session_id() to check whether the session has started. 1) Use the session_status() function. If PHP_SESSION_ACTIVE is returned, the session has been started. 2) Use the session_id() function, if a non-empty string is returned, the session has been started. Both methods can effectively check the session state, and choosing which method to use depends on the PHP version and personal preferences.

Sessionsarevitalinwebapplications,especiallyfore-commerceplatforms.Theymaintainuserdataacrossrequests,crucialforshoppingcarts,authentication,andpersonalization.InFlask,sessionscanbeimplementedusingsimplecodetomanageuserloginsanddatapersistence.

Managing concurrent session access in PHP can be done by the following methods: 1. Use the database to store session data, 2. Use Redis or Memcached, 3. Implement a session locking strategy. These methods help ensure data consistency and improve concurrency performance.

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
