Jump to: navigation, search

Php md5

From w3cyberlearnings

Contents

PHP function md5

This function calculates the MD5 hash of a given string.

Syntax md5

  • String: String input to be calculated for the MD5 hasing.
  • raw(optional): True (16 character binary format), False (32 character hex number) "Default"
md5(string, raw)

Example 1

  • Without the second argument (raw), use the default raw option (False).

<?php

echo md5('Hello');
//echo md5('Hello',false); the same
?>

Output

8b1a9953c4611296a827abf8c47804d7

Example 2

  • Set raw to false, and this used 16 characters binary format.
<?php
echo md5('Hello',false);
?>

Output

‹�™SÄa�–¨'«øÄx�×

Example 3: Encrypt password using md5

<?php
$password="4@fdPassowr$243e";

$my_encrypt_pass = md5($password);
echo $my_encrypt_pass;
?>

Output

b0aa1bf5928950eb6dd2b78b272d6489

Example 4: Check user password input with MD5 encryption

<?php

$password = "4@fdPassowr$243e";

$_POST['password'] = $password;

$my_encrypt_pass = md5($password);

if (md5($_POST['password']) == 'b0aa1bf5928950eb6dd2b78b272d6489') {
	echo 'Password matched';
} else {
	echo 'Not matched';
}
?>

Output

Password matched

Example 5: Encrypt user name and password with MD5 encryption

<?php

$username = $_POST['login_name'] = 'David';
$password = $_POST['login_pass'] = 'password30$$@';

$secret_password = md5($username.$password);
echo $secret_password;
?>

Output

3e3b6a8679dd7f05ad543f8834b35809

Related Functions


Navigation
Web
SQL
MISC
References