PHP user development registration module PHP page

The main functions of user registration were introduced earlier. This page implements these functions through PHP code.

We use POST method to obtain data.

<?php
$username = $_POST['username']; //注册的用户名
$password = $_POST['password'];  //注册密码
$confirm = $_POST['confirm'];  //确认密码
$email = $_POST['email'];  //邮箱
$code = $_POST['code'];   //验证码
?>

Continue to connect to the database and table we have created

<?php
$link = mysqli_connect('localhost','root','root','test');
if (!$link) {
  die("连接失败:".mysqli_connect_error());
}
$sql = "select * from login";
$result = mysqli_query($link, $sql);
$rows = mysqli_fetch_array($result);
?>

We need to make regular judgments on the entered user name and email address

<?php
if ((strlen($username) < 3)||(!preg_match('/^\w+$/i', $username))) 
{  
     echo "<script>alert('用户名至少3位且不含非法字符!重新填写');window.location.href='zhuce'</script>";  
     //判断用户名长度和非法字符
}
if (!preg_match('/^[\w\.]+@\w+\.\w+$/i', $email)) 
{    
     echo "<script>alert('邮箱不合法!重新填写');window.location.href='zhuce.html'</script>";    
     //判断邮箱格式是否合法
}
?>

The biggest difference here from user login is if The username has been registered by another user and you will no longer be able to use this username.

You need to read the username data that already exists in the database first, and then make a judgment.

<?php
if(mysqli_fetch_array(mysqli_query($link,"select * from login where username = '$username'")))
{     
    echo "<script>alert('用户名已存在');window.location.href='zhuce.html'</script>";
    // 判断用户名是否已经被注册
}
?>

Complete zhuce.php file code:

<?php
  session_start();
  header("Content-type:text/html;charset=utf-8");
  $link = mysqli_connect('localhost','root','root','test');
  if (!$link) {
    die("连接失败:".mysqli_connect_error());
  }
  
  $username = $_POST['username'];
  $password = $_POST['password'];
  $confirm = $_POST['confirm'];
  $email = $_POST['email'];
  $code = $_POST['code'];
  
  if($username == "" || $password == "" || $confirm == "" || $email == "" || $code == "")
  {
    echo "<script>alert('信息不能为空!重新填写');window.location.href='zhuce.html'</script>";
  } elseif ((strlen($username) < 3)||(!preg_match('/^\w+$/i', $username))) {
    echo "<script>alert('用户名至少3位且不含非法字符!重新填写');window.location.href='zhuce'</script>";
    //判断用户名长度
  }elseif(strlen($password) < 5){
      echo "<script>alert('密码至少5位!重新填写');window.location.href='zhuce.html'</script>";
      //判断密码长度
  }elseif($password != $confirm) {
      echo "<script>alert('两次密码不相同!重新填写');window.location.href='zhuce.html'</script>";
      //检测两次输入密码是否相同
  } elseif (!preg_match('/^[\w\.]+@\w+\.\w+$/i', $email)) {
      echo "<script>alert('邮箱不合法!重新填写');window.location.href='zhuce.html'</script>";
      //判断邮箱格式是否合法
  } elseif($code != $_SESSION['authcode']) {
    echo "<script>alert('验证码错误!重新填写');window.location.href='zhuce.html'</script>";
    //判断验证码是否填写正确
  } elseif(mysqli_fetch_array(mysqli_query($link,"select * from login where username = '$username'"))){
    echo "<script>alert('用户名已存在');window.location.href='zhuce.html'</script>";
  } else{
    $sql= "insert into login(username, password, confirm, email)values('$username','$password','$confirm','$email')";
    //插入数据库
    if(!(mysqli_query($link,$sql))){
      echo "<script>alert('数据插入失败');window.location.href='zhuce.html'</script>";
    }else{
      echo "<script>alert('注册成功!)</script>";
    }
  }
?>

Enter the registration page, fill in all the correct data, and then open the database and you will find that the data you just added already exists.

2.png

For example: Here we add a user with a username of sell, password and confirmation password of 12345, and an email address of 123@www.com.

Note: This course only briefly demonstrates user registration. Its code is for learning reference only and cannot be used directly in projects.

Continuing Learning
||
<?php session_start(); header("Content-type:text/html;charset=utf-8"); $link = mysqli_connect('localhost','root','root','test'); if (!$link) { die("连接失败:".mysqli_connect_error()); } $username = $_POST['username']; $password = $_POST['password']; $confirm = $_POST['confirm']; $email = $_POST['email']; $code = $_POST['code']; if($username == "" || $password == "" || $confirm == "" || $email == "" || $code == "") { echo "<script>alert('信息不能为空!重新填写');window.location.href='zhuce.html'</script>"; } elseif ((strlen($username) < 3)||(!preg_match('/^\w+$/i', $username))) { echo "<script>alert('用户名至少3位且不含非法字符!重新填写');window.location.href='zhuce'</script>"; //判断用户名长度 }elseif(strlen($password) < 5){ echo "<script>alert('密码至少5位!重新填写');window.location.href='zhuce.html'</script>"; //判断密码长度 }elseif($password != $confirm) { echo "<script>alert('两次密码不相同!重新填写');window.location.href='zhuce.html'</script>"; //检测两次输入密码是否相同 } elseif (!preg_match('/^[\w\.]+@\w+\.\w+$/i', $email)) { echo "<script>alert('邮箱不合法!重新填写');window.location.href='zhuce.html'</script>"; //判断邮箱格式是否合法 } elseif($code != $_SESSION['authcode']) { echo "<script>alert('验证码错误!重新填写');window.location.href='zhuce.html'</script>"; //判断验证码是否填写正确 } elseif(mysqli_fetch_array(mysqli_query($link,"select * from login where username = '$username'"))){ echo "<script>alert('用户名已存在');window.location.href='zhuce.html'</script>"; } else{ $sql= "insert into login(username, password, confirm, email)values('$username','$password','$confirm','$email')"; //插入数据库 if(!(mysqli_query($link,$sql))){ echo "<script>alert('数据插入失败');window.location.href='zhuce.html'</script>"; }else{ echo "<script>alert('注册成功!)</script>"; } } ?>
submitReset Code