温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用PHP怎么编写一个用户注册表单验证功能

发布时间:2021-01-13 15:05:53 来源:亿速云 阅读:198 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关利用PHP怎么编写一个用户注册表单验证功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

具体如下:

注册界面

利用PHP怎么编写一个用户注册表单验证功能

register.html

  <h2>用户注册</h2>
  <form method="post" action="register_verify.php">
    <input type="text" placeholder="用户名" name="username"><br><br>
    <input type="password" placeholder="密码" name="password"><br><br>
    <input type="password" placeholder="重复密码" name="password2"><br><br>
    <label>性别:
      <input type="radio" name="sex" value="男" checked="checked">男
      <input type="radio" name="sex" value="女">女</label><br><br>
    <input type="email" placeholder="邮箱" name="email"><br><br>
    <button class="btn" type="submit">注册</button>
  </form>

register_verify.php

<?php
require "mysql.php";      //导入mysql.php访问数据库
$username=$_POST['username'];
$password=$_POST['password'];
$password2=$_POST['password2'];
$sex=$_POST['sex'];
$email=$_POST['email'];
if(checkEmpty($username,$password,$password2,$sex,$email)){
  if(checkpwd($password,$password2)){
    if(checkEmail($email)){
      if(insert($username,$password,$sex,$email))
        echo"注册成功";
    }
  }
}
//方法:判断是否为空
function checkEmpty($username,$password,$password2,$sex,$email){
  if($username==null||$password==null||$password2==null){
    echo '<html><head><Script Language="JavaScript">alert("用户名或密码为空");</Script></head></html>'       . "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">";
  }
  else{
    if($sex==null){
      echo '<html><head><Script Language="JavaScript">alert("性别为空");</Script></head></html>' .          "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">";
    }
    elseif($email==null){
      echo '<html><head><Script Language="JavaScript">alert("邮箱为空");</Script></head></html>' .          "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">";
    }
    else{
      return true;
    }
  }
}
//方法:检查两次密码是否相同
function checkpwd($password,$password2){
  if($password==$password2)
    return true;
  else
    echo '<html><head><Script Language="JavaScript">alert("两次密码不一致");</Script></head></html>' .        "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">";
}
//方法:邮箱格式验证
function checkEmail($email){
  $preg = '/^(\w{1,25})@(\w{1,16})(\.(\w{1,4})){1,3}$/';
  if(preg_match($preg, $email)){
    return true;
  }else{
    echo '<html><head><Script Language="JavaScript">alert("邮箱格式有误");</Script></head></html>' .        "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">";
  }
}
//方法:将数据插入数据库中
function insert($username,$password,$sex,$email){
  $conn=new Mysql();
  $sql="insert into user VALUE (null,'$username','$password','$sex','$email')";
  $result=$conn->sql($sql);
  if($result){
    return true;
  }
  else{
    echo '<html><head><Script Language="JavaScript">alert("写入数据库失败");</Script></head></html>' .        "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">";
  }
  $conn->close();
}

看完上述内容,你们对利用PHP怎么编写一个用户注册表单验证功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI