Cookie information security for user login
Cookie information security for user login
I. cookie information encryption method the cookie information encryption method uses an encryption method to encrypt user information and then stores it in the cookie, in this way, even if a cookie is obtained, the counterfeiter can only exploit the cookie within the cookie validity period. Here is an encryption function: 0) & substr ($ result, 10, 16) = substr (md5 (substr ($ result, 26 ). $ keyb), 0, 16) {return substr ($ result, 26) ;}else {return '';}} else {// Save the dynamic key in the ciphertext, this is also the reason why different ciphertext texts can be decrypted in the same plain text. // because the encrypted ciphertext may be special characters, the replication process may be lost, therefore, return $ keyc is encoded in base64 format. str_replace ('=', '', base64_encode ($ result); }}$ str = 'abcdef '; $ key = 'www .phpskill.com '; echo $ jm = authcode ($ str, 'encoding', $ key, 0); // encrypt echo ""; e Cho authcode ($ jm, 'Decode', $ key, 0); // decryption?> In this way, when a user information cookie is set, it cannot be forged: $ uid, "username" => $ username); $ user = base64_encode (serialize ($ user )); $ user = authcode ($ user, 'encoding', 'www .phpskill.com ', 0); // encrypt setcookie ("user", $ user, time () + 3600*24);?> II. use an encryption token to protect the cookie $ hash = md5 ($ uid. time (); // encryption token value $ hash_expire = time () + 3600*24; // The encryption token value is valid for one day $ user = array ("uid" => $ uid, "username" => $ username, "hash" => $ hash ); $ user = base64_encode (serialize ($ user); setcookie ("user", $ user, $ hash_expr ); then, save $ hash and $ hash_expire to the corresponding fields of hash and hash_expire in the member table, and store them in nosql. when session users forge cookies, the hash cannot be forged, the forged hash is inconsistent with that in the database. the hash value is not updated during the hash_expire validity period and is updated upon expiration.
The above is the cookie information security content for user login. For more information, please follow the PHP Chinese network (www.php1.cn )!