In this article you are going to learn how you can encrypt the username and password using functions in php. Well, encryption provide data security by converting the sensitive information or data into a code to prevent uncertified access of recipients.
Today, almost each and every application uses encryption to prevent sensitive information of its users from illicit recipients. If one wants to log into his account he must use the username and password set by himself to have access to his account. The other person will not be able to access the account unless he knows the password.
The functions which are generally used to encrypt the username and password in php are md5(), sha1() and base64_encode. These are described briefly with examples in the below section. During Our Php course students go through several examples, few are listed below.
ENCRYPTION USING MD5() FUNCTION IN PHP
Syntax : md5(string);
Example 1 : An example is shown below using md5() function to encrypt the password. Here we have used ‘W3 Training school’ as password.
Output : You will get the following output on your screen. This output will remain the same unless the password is changed.
Description : First, we have taken a variable ‘$a’ whose value is ‘W3 Training school’ which is the password. Then we have encrypted the password using md5() function and stored this value inside a new variable ‘$b’. At last we have used echo statement for ‘$b’ to print the desired output. Clearly, after encryption the information is converted into a code which is highly secured.
Example 2 : An example of login password is shown below.
Output : You will get the following output on your screen.
Description : In this example we have taken a third variable ‘$c’ whose value is ‘Password entered by the user’. It means when the user enter the password, it will store in the variable ‘$c’. If the password entered by the user ‘$c’ matches the encrypted password ‘$b’, then the output will be ‘Welcome User’. But if the password do not match with the encrypted password, the output will be ‘Wrong Password’. We have used ‘if’ and ‘else’ command to perform this function.
ENCRYPTION USING SHA1() FUNCTION IN PHP
Syntax : sha1(string);
It is used in the same way as md5 function does. The examples are shown below.
Example 1 : A simple example to encrypt the password using sha1() function is shown below.
Output : The following output will be generated
Example 2 :
Output :
ENCRYPTION USING BASE64_ENCODE IN PHP
Syntax : For encryption-base64_encode(string);
For Decryption- base64_decode(string);
Example 1: In the example given below we have used base64_encode to encrypt the password.
Output : After encryption, we will get the following output on the screen.
Description : We have used base64_encode to encrypt the password ‘w3 training school’ whose value is stored in the variable $a. Then echo statement has been used to print the desired output.
Example 2 : In this example, we have used base64_decode to decrypt(convert the encrypted data or code into the text) the password so that it can again become readable to the user or the computer.
Output : The code will be converted into the readable text which is the password as shown below.
Description : We have taken a second variable ‘$b’ to store the value of the decrypted code by using base64_decode function. Then echo statement has been used to print the output of ‘$b’.