首页  >  文章  >  后端开发  >  PHP MD5()

PHP MD5()

WBOY
WBOY原创
2024-08-29 12:52:57694浏览

PHP编程语言的MD5()函数会产生字符串的哈希值,就像编码过程一样。 MD5() 函数仅适用于 PHP 4、5、7 版本,但对于其他 PHP 版本,哈希编码器“md5()”可能有效,也可能大部分无效。大多数情况下,不建议使用 md5() 函数来安全地保护密码,因为该函数借助其内置哈希算法进行快速编码。它只接受两个参数。在这两者中,只有一项始终是强制性的。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

String md5 ($string, $getRawOutput)

参数简单说明:

PHP 编程语言的 MD5() 函数最多接受两个参数。它们是:$string参数和$getRawOutput参数。

  • $string: $string 参数将帮助我们期望字符串被散列。
  • $getRawOutput: $getRawOutput 参数将帮助我们期望一个布尔值。对于 TRUE 结果,该函数将以原始二进制格式返回长度为 16 的 HASH。

返回类型:PHP 的 md5() 函数将返回散列字符串(它可以是长度为 32 的小写十六进制格式字符序列(32 个字符的十六进制数字)或原始二进制文件长度为 16) 的形式。

MD5() 函数在 PHP 中如何工作?

PHP 编程语言的 MD5() 函数目前适用于 PHP 4、PHP 5 和 PHP 7 版本。除了这些版本之外,md5() 函数大多数情况下可能不起作用。它是一个内置函数,通过使用 md5() 函数,我们在 PHP 编程语言内部启动 HASHING 算法。通过后端哈希算法,可以根据需要完成特定数值/字符串值/任何其他值的哈希转换。这对于编码过程非常有帮助。 MD5() 函数值将始终采用 32 位二进制格式,除非在 md5() 函数内部使用第二个参数。此时 md5() 值将是 16 位二进制格式。

实现 PHP md5() 的示例

以下是示例:

示例#1

在下面的示例中,使用字符串值“apples”创建变量“$str1”。然后使用 print 函数打印一些字符串文本。之后,创建“$a1”变量并将其分配给 md5() 函数以及 md5() 函数内部的“$str1”变量。然后使用 echo 函数通过打印“$a1”变量值来打印更改后的哈希结果。
用于换行和


在下面提到的所有 PHP 代码中用于水平线。 IF 条件通过传递一些哈希结果来使用。与原始字符串的哈希结果相比,哈希结果为 false。因此,ELSE 条件结果将在 ECHO 函数的帮助下打印出来。

代码:

<?php
$str1 = 'apples';
print "This is the value of HASH of apples :: ";
$a1 = md5($str1);
echo "$a1 <br>";
print "<hr>";
if (md5($str1) === '1f3870be274f6c49b3e31a0c6728957f') {
echo "If the value of apples is :: 1f3870be274f6c49b3e31a0c6728957f then it will print :: ";
echo "<br>Your condition is TRUE so";
echo "<br> Would you like a green or red apple?<br><hr>";
}
else{
echo "<br> Your input for IF conditioni is FALSE";
}
?>

输出:

PHP MD5()

示例#2

在下面的示例中,“$input_string1”变量是使用值“Pavan Kumar Sake”创建的。然后使用 echo 函数打印原始字符串。 “
”和


仅用于换行符和水平线。使用 md() 函数并将其存储在“$i1”变量中后,可以打印原始字符串的 16 位二进制格式。仅通过使用 md5() 函数内部的两个参数即可完成。然后再次使用 md5() 函数,并且仅通过函数内部仅使用一个参数来完成。这里将使用 echo 函数打印由 md5() 形成的 32 位二进制格式哈希码。

代码:

<?php
$input_string1 = 'Pavan Kumar Sake';
echo 'Original string :: '.$input_string1.'<br><hr>';
echo '16 bit binary format :: ';
$i1 = md5($input_string1,TRUE);
echo $i1;
echo '<br><hr>';
echo '32 bit binary format :: '.md5($input_string1).'<br><hr>';
?>

输出:

PHP MD5()

示例#3

在下面的示例中,我将使用 FOR 循环实现 0-10 之间的数值的哈希码。首先,在 PHP 标签内,使用数值 10 创建变量“$k”。然后使用 $i 值创建 FOR LOOP,用于初始化、条件和递增值。循环将从 0 值开始,到 10 值结束。在循环内部 md5() 函数与内部的 $i 变量值一起使用。因此,当循环运行时,将计算每个 $i 变量的值 md5($i) 并打印特定数值的哈希代码的输出。然后“
”用于换行,以便更好地输出 LOOP 元素。

代码:

<?php
$k = 10;
for($i=0;$i<=$k;$i++){
print "Hash code of $i :: ";
print md5($i);
echo "<br>";
}
?>

输出:

PHP MD5()

Example #4

In the below example, username and password checking conditions are involved inside of the PHP tags. At first, “$user1” variable and “$pass1” variable is created with string values inside. Then md5() functions are used to encode the “$user1” and “$pass1” variable’s values. Then by using the echo function hash codes of the variables are printed. Then “


” tag is used to print the horizontal with the help of echo function. Then IF and ELSE conditions are made to check the variables hash code values. If the hash code values of the “$user1” and “$pass1” are exactly equal to the string values passed in the IF conditions then IF conditions will become TRUE and will print some string statements which are mentioned using echo function. If the IF condition becomes FALSE then ELSE statements will be printed.

Code:

<?php
$user1 = "Pavan Kumar Sake";
$pass1 = "pavansake123";
$user1_encode = md5($user1);
$pass1_encode = md5($pass1);
echo "$user1 has hash code ::  $user1_encode <br>";
echo "$pass1 has hash code ::  $pass1_encode <br>";
echo "<hr>";
if (md5($user1)== "4c13476f5dd387106a2a629bf1a9a4a7"){
echo "Username is correct<br>";
if(md5($pass1)== "20b424c60b8495fae92d450cd78eb56d"){
echo "Password is also correct so login will be successful";
}
else{
echo "Incorrect Password is entered";
}
}
else{
echo "Incorrect Username is entered";
}
echo "<hr>";
?>

Output:

PHP MD5()

Conclusion

I hope you understood what is the definition of PHP md5() function with the syntax and its explanation, Info regarding the parameters in brief detail, Working of md5() function in PHP along with the various examples to understand the concept well.

以上是PHP MD5()的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:PHP chop()下一篇:PHP unset()