Use crypt () in PHP to verify user identity

Source: Internet
Author: User
If you do not want to develop new encryption algorithms, you can also use the crypt () function provided by PHP to implement one-way encryption. Readers who know about crypt () may be familiar with crypt () as long as they have a little experience in using non-Windows platforms. if they do not want to develop new encryption algorithms, you can also use the crypt () function provided by PHP to complete one-way encryption.

   Understanding crypt ()

Readers who have some experience in using non-Windows platforms may be quite familiar with crypt (). This function is called one-way encryption, which can encrypt some plain codes, however, the password cannot be converted to the original plaintext. The crypt () function is defined as follows.

String crypt (string input_string [, string salt])

The input_string parameter is the plaintext string to be encrypted, and the second available salt is a single-digit string, which can affect the encrypted password and further eliminate the possibility of cracking. By default, PHP applies a two-character DES interference string. if the system applies MD5 (refer to the next section ), PHP will apply a 12-character interference string. You can execute the following command to invent the length of the interference string to be applied by the system.

Print 'My system salt size is: '. CRYPT_SALT_LENGTH;

Crypt () supports four encryption algorithms. Table 19.1 shows the supported algorithms and the length of corresponding salt parameters.

Table crypt () supports four encryption algorithms

Algorithms Salt length CRYPT_STD_DES2-character (Default) CRYPT_EXT_DES9-characterCRYPT_MD512-character beginning with $1 $ CRYPT_BLOWFISH16-character beginning with $2 $
On the surface, the crypt () function seems useless, but it is indeed widely used to ensure the integrity of the system password. Because, even if the one-way encryption password falls into the hands of a third party, it is useless because it cannot be restored to plain text.

   Use crypt () for user authentication

The previous section briefly introduces the functions of the crypt () function, and uses it to authenticate the user's identity. the objectives to be achieved are the same as those described in section 19.2.3.

1 <! -- Check_user_crypt.php: Use the crypt () function to verify the user -------------->
2 <? Php
3 $ user_name = $ _ POST ['User _ name'];
4 require_once ('sys _ conf. Inc'); // system configuration file, including database configuration information
5
6 // connect to the database
7 $ link_id = mysql_connect ($ DBHOST, $ DBUSER, $ DBPWD );
8 mysql_select_db ($ DBNAME); // select the database my_chat
9
10 // query for logon user information
11 $ str = 'SELECT name, password from user where name = '$ user_name '';
12 $ result = mysql_query ($ str, $ link_id); // perform the query
13 @ $ rows = mysql_num_rows ($ result); // number of records that have obtained the query results
14 $ user_name = $ _ SESSION ['User _ name'];
15 $ password = $ _ POST ['password'];
16 $ salt = substr ($ password, 0, 2 );
17 $ password_en = crypt ($ password, $ salt); // use crypt () to encrypt the user password.
18
19 // for old users
20 if ($ rows! = 0)
21 {
22 list ($ name, $ pwd) = mysql_fetch_row ($ result );
23
24 // if the password is accurate
25 if ($ pwd = $ password_en)
26 {
27 $ str = 'update user set is_online = 1 where name = '$ user_name' and password = '$ password_en '';
28 $ result = mysql_query ($ str, $ link_id); // perform the query
29 require ('main. php'); // go to the chat page
30}
31 // password input error

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: info-contact@alibabacloud.com 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.