Cookie information security for user login-tutorial on cookie information security _ PHP

Source: Internet
Author: User
Cookie information security: cookie information security. Cookie information security for user login: cookie information security everyone knows that after a user logs on, the user information is generally stored in the cookie, because the cookie is saved on the client, cookie information security: cookie information security

We all know that after a user logs in, the user information is usually stored in the cookie, because the cookie is used to save the client,
In addition, the cookie can be freely changed in the browser on the client side, which may cause the risk of forgery of the user's cookie, which may cause the counterfeit cookie to log on to any user's account.

The following describes some common methods to prevent users from logging on to cookie information security:

I. cookie information encryption method
The cookie information encryption method uses an encryption method to encrypt user information and then store the information in the cookie. This way, even if a cookie is obtained, the counterfeiter can only exploit the cookie within the cookie validity period, cookie information cannot be forged.

An encryption function is attached here:

 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 ""; echo authcode ($ Jm, 'Decode', $ key, 0); // decrypt?>

In this way, the cookie of user information 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 cookies

$ 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.

Php pure technology exchange group: 323899029

Original article reproduced in:

Everyone knows that after a user logs in, the user information is usually stored in the cookie, because the cookie is saved on the client ,...

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.