Home > Backend Development > PHP Tutorial > Example of using crypt to encrypt the password when registering a PHP user

Example of using crypt to encrypt the password when registering a PHP user

怪我咯
Release: 2023-03-12 19:12:01
Original
1849 people have browsed it

This article mainly introduces php to implement User registrationcrypt encryption of passwords in detail. It has a certain reference value. Interested friends can refer to the examples in this article

I have shared with you the encryption of PHP user registration password for your reference. The specific content is as follows

1. Code

1. conn.php

<?php 
  $conn = mysql_connect("localhost", "root", "111") or die("连接数据库服务器失败!".mysql_error()); //连接MySQL服务器 
  mysql_select_db("db_database21",$conn);     //选择数据库db_database21 
  mysql_query("set names utf8");           //设置数据库编码格式utf8 
?>
Copy after login

2. index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>用户注册</title> 
<style type="text/css"> 
<!-- 
body,td,th { 
  font-size: 12px; 
} 
--> 
</style></head> 
<body> 
<form id="form1" name="form1" method="post" action="index_ok.php"> 
 <fieldset style="width:500px"><legend style="font-size:16px">用户注册</legend><table width="300" border="0" align="center"> 
  <tr> 
   <td width="77" align="right">用户名:</td> 
   <td width="213"><input name="user" type="text" id="user" size="24" /></td> 
  </tr> 
  <tr> 
   <td align="right">密码:</td> 
   <td><input name="pwd" type="password" id="pwd" size="25" /></td> 
  </tr> 
  <tr> 
   <td> </td> 
   <td><input type="submit" name="sub" value="注册" /> 
     <input type="reset" name="res" value="重置" /></td> 
  </tr> 
 </table></fieldset> 
</form> 
</body> 
</html>
Copy after login

3. index_ok.php

<?php 
  header("content-type:text/html;charset=utf-8"); //加载头信息 
  include("conn/conn.php");//包含数据库连接文件 
  if(trim($_POST[&#39;user&#39;])!= "" and trim($_POST[&#39;pwd&#39;])!= ""){//判断输入是否为空 
    $pwd = crypt($_POST[&#39;pwd&#39;],"key");//对输入的密码进行crypt加密 
    $sql = "insert into tb_user(username,password)values(&#39;".$_POST[user]."&#39;,&#39;". $pwd."&#39;)";//定义sql语句 
    $result = mysql_query($sql,$conn);//执行sql语句 
    if($result){ 
      echo "<font color=&#39;red&#39;>注册成功。</font>";//如果结果为真提示注册成功 
    }else{ 
      echo "<font color=&#39;green&#39;>注册失败!</font>";//否则提示注册失败 
    } 
  }else{ 
    echo "请认真填写用户名和密码!";//提示输入用户名和密码 
  } 
?>
Copy after login

2. Running results

After successful registration, the database displays as follows:

The above is the detailed content of Example of using crypt to encrypt the password when registering a PHP user. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template