Cookie information security for user login-PHP source code

Source: Internet
Author: User
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 '; 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 ', 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 ( )!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.