Home > Article > Backend Development > A few things about cookie information security for user login, cookie information security_PHP tutorial
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