PHP cryptographic function-md5 () function Encryption instance usage

Source: Internet
Author: User
Tags crypt md5 encryption md5 hash sha1
PHP Cryptographic function-md5 () function Encryption

What is the MD5 () function?

The MD5 () function is the MD5 hash value of the calculator string, using the MD5 algorithm, MD5 's full name is Message-digest algorithm 5, which is the function of the different lengths of data information through a series of algorithms to calculate a 128-bit value, is to turn an arbitrary-length byte string into a long, large integer. Note that this is a "byte string", not a "string" because the transformation is only related to the value of the byte, not the character set or encoding.

We introduced in the previous chapter for you the PHP cryptographic function-crypt () function encryption, I believe that you already have some knowledge of PHP cryptographic functions, today, we give you a detailed introduction to the PHP cryptographic function-MD5 () function encryption .

First, let's look at the syntax structure of the MD5 () function:

String MD5 (String Str[,bool raw_output]);

Where the string str is the plaintext to be encrypted, and if the Rew_output parameter is set to True, then the function returns a binary form of the ciphertext, which defaults to false.

In many web sites, the password of the registered user name is used MD5 encryption, and then save to the database, the user name log in, the program to the user input area calculated as MD5 value, and then to the database to save the MD5 value to compare, in this process, the program itself will not "know" the user's real password , so as to ensure the personal privacy of the users of the ancestral book, improve security.

The following example implements the function of registration and login, after MD5 encryption, save the database, the following steps:

The first step: Create a conn.php file and complete the link to the database:

<?phpheader ("content-type:text/html; Charset=utf-8 "); $conn =mysql_connect (" localhost "," root "," root ") or Die (" Database connection Failed ". Mysql_error ());//Connection Server Mysql_ SELECT_DB ("Here is your database name", $conn);//Connect Database mysql_query ("set name gb2312");//Set encoding format?>

The second step: Create a Member registration page, is the register.php file, in the file, first create form form, through the register () method to verify the value of the form element, and then add the form element, complete the user name and password submission, Finally submits the data in the form to the register_ok.php file, through the object-oriented method completes the registration information The submission operation, here does not post the code!

The third step is to create the register_ok.php file, get the data in the form, encrypt the password through the MD5 () function, and use the object-oriented method to complete the reference code as follows:

<?phpheader ("content-type:text/html;                       Charset=utf-8 "), class chkinput{//define Chkinput class Var $name;                      Defines the member variable Var $pwd;           Define member variable function chkinput ($x, $y) {//define member method $this, name = $x;            Assign a value to a variable $this pwd = $y;           Assign value to variable} function Checkinput () {//define method include "conn/conn.php"; Call File $info = mysql_query ("INSERT into here is the database name (User,password) value ('". $this, ".", ". $this, pwd.")    if ($info = = False) {echo "<script language= ' JavaScript ' >alert (' member registration failed '); History.back ();</script>"; Exit ();}    else{$_session[admin_name]= $this->name; echo "<script language= ' JavaScript ' >alert (' member registration succeeded '); window.location,href= ' index.php ';</script> ';}}} $obj = new Chkinput (Trim ($_post[name]), Trim (MD5 ($_POST[PWD)));                                             Instantiation Class $obj-checkinput (); Back to?>

Fourth step: Create index.php and index_ok.php files, realize the function of login, not paste the code here!

After the completion of the database can then view the password after encryption!

In the next article, I'll show you a function similar to the MD5 () function, SHA1 (), read the PHP cryptographic function-sha1 () function Encryption!


"Recommended"

1. Related topics: "PHP cryptographic Functions"

2.PHP cryptographic function-crypt () function Encryption usage instance

3.PHP cryptographic function-sha1 () function Encryption instance usage

4.PHP Encryption Technology Video tutorial

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