当前位置:Gxlcms > PHP教程 > php使用crypt()函数进行加密

php使用crypt()函数进行加密

时间:2021-07-01 10:21:17 帮助过:47人阅读

这篇文章主要为大家详细介绍了php使用crypt()函数进行加密,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、代码

  1. <?php
  2. $str = '应用crypt()函数进行单向加密!'; //声明字符串变量$str
  3. echo '加密前$str的值为:'.$str;
  4. $crypttostr = crypt($str); //对变量$str加密
  5. echo '<p>加密后$str的值为:'.$crypttostr; //
输出加密后的变量 ?>

二、运行结果

参数不带salt,每次加密得出的密文都不一样。
加密前$str的值为:应用crypt()函数进行单向加密!
加密后$str的值为:$1$Re4.Gg4.$D.yd00xX0fFfIfp6KrKGN0

三、代码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>使用crypt函数进行数据验证</title>
  6. <style type="text/css">
  7. <!--
  8. body,td,th {
  9. font-size: 12px;
  10. }
  11. body {
  12. margin-left: 10px;
  13. margin-top: 10px;
  14. margin-right: 10px;
  15. margin-bottom: 10px;
  16. }
  17. .STYLE1 {
  18. font-size: 14px;
  19. font-weight: bold;
  20. }
  21. -->
  22. </style>
  23. </head>
  24. <body>
  25. <p align="center">
  26. <?php
  27. $conn = mysql_connect("localhost","root","root") or die("数据库链接错误".mysql_error());
  28. mysql_select_db("db_database21",$conn) or die("数据库访问错误".mysql_error());
  29. mysql_query("set names gb2312");
  30. ?>
  31. </p>
  32. <table width="777" height="587" border="0" align="center" cellpadding="0" cellspacing="0" background="images/bg.jpg">
  33. <tr>
  34. <td width="149" height="200"> </td>
  35. <td width="448"> </td>
  36. <td width="158"> </td>
  37. </tr>
  38. <tr>
  39. <td height="187"> </td>
  40. <td align="center" valign="middle"> <form id="form1" name="form1" method="post" action="index.php">
  41. <table height="129" border="0" cellpadding="0" cellspacing="0">
  42. <tr>
  43. <td width="100" height="30" align="right" valign="middle" scope="col"><span class="STYLE1">用户名:</span></td>
  44. <td width="100" height="30" align="left" valign="middle" scope="col"><label for="textfield"></label>
  45. <input name="username" type="text" id="username" size="24" /></td>
  46. <td width="100" align="center" valign="middle" scope="col"> </td>
  47. </tr>
  48. <tr>
  49. <td height="30" align="right" valign="middle" class="STYLE1" scope="col">密码:</td>
  50. <td height="30" align="left" valign="middle" scope="col"><input name="password" type="password" id="password" size="25" /></td>
  51. <td align="center" valign="middle" scope="col"> </td>
  52. </tr>
  53. <tr>
  54. <td height="40" colspan="3" align="center" valign="middle" scope="col"><input type="image" name="imageField" src="images/bg2.JPG" />
  55. <input type="image" name="imageField2" src="images/bg1.JPG" onclick="form.reset();return false;" /></td>
  56. </tr>
  57. </table>
  58. </form>
  59. <?php
  60. if(trim($_POST[username])!= "" and trim($_POST[password])!= ""){
  61. $usr = crypt(trim($_POST[username]),$_POST[username]);
  62. $pwd = crypt(trim($_POST[password]),$_POST[password]);
  63. $sql = "select * from tb_user where username = '".$usr."' and password='".$pwd."'";
  64. $rst = mysql_query($sql,$conn);
  65. $result=mysql_num_rows($rst);
  66. if($result>0){
  67. echo "<font color='red'>用户登录成功。</font>";
  68. }else{
  69. echo "<font color='green'>用户登录失败!</font>";
  70. }
  71. }else{
  72. echo "请认真填写用户名和密码!";
  73. }
  74. ?></td>
  75. <td> </td>
  76. </tr>
  77. <tr>
  78. <td height="200"> </td>
  79. <td> </td>
  80. <td> </td>
  81. </tr>
  82. </table>
  83. </body>
  84. </html>


四、运行结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

以上就是php使用crypt()函数进行加密的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行