Skip to content

How To Encrypt The Username And Password Using PHP Code?

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);

php-md5encode-1024x513

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.

php-md5encodeoutput-1024x305

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.

php-md5decode-1024x515

Example 2 : An example of login password is shown below.

Output : You will get the following output on your screen.

php-md5decodeoutput-1024x341

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.

php-sha1encode-1024x514

Example 1 : A simple example to encrypt the password using sha1() function is shown below.

php-sha1encodeoutput-1024x346

Output : The following output will be generated

php-sha1decode-1024x515

Example 2 :

php-sha1decodeoutput-1024x344

Output :

ENCRYPTION USING BASE64_ENCODE IN PHP

Syntax : For encryption-base64_encode(string);
For Decryption- base64_decode(string);

php-base64encode-1024x511

Example 1: In the example given below we have used base64_encode to encrypt the password.

php-base64encodeoutput-1024x284

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.

php-base64decode-1024x515

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.

php-base64decodeoutput-1024x293

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

Facebook
Twitter
LinkedIn
Pinterest