php用户注册表单验证

注册界面


register.html

    <h1>用户注册</h1>
    <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();
}

验证码和数据库的实现方法前面写过,这里不再赘述。

验证码制作:http://blog.csdn.net/c_jian/article/details/52794742

数据库连接:http://blog.csdn.net/c_jian/article/details/52847499


  • 2
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值