PHP+AJAX实现账号注册和登陆,附可用demo

前言

登陆和注册已经是网站的标配了,所以这是我们web开发学习过程中必学的了。其实很容易实现,只需要懂数据库的增删改查,还有if_else的条件语句即可做出来。

目录

css
    --Login-Reg.css
    
server
    --LoginUser.php
    --RegUser.php
    
--LoginFrom.html
--RegForm.html

注册

RegForm.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户注册</title>
    <link rel="stylesheet" type="text/css" href="css/Login-Reg.css">
</head>
<body>
    <h2 id="Reg-text">用户注册</h2>
    <div id="Form-con">
    <form id="form1" onsubmit="return false" action="##" method="post">
        <input type="text" name="username" class="inputStyle" placeholder="请输入账号"><br/>
        <input type="password" name="password" class="inputStyle" placeholder="请输入密码"><br/>
        <input type="password" name="cpassword" class="inputStyle" placeholder="请确认密码"><br/>
        <input type="submit" class="btnStyle" value="注册" onclick="RegUser()">
    </div>
    <div id="Result"></div>
    </form>
</body>
<!-- AJAX提交表单 -->
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript">
        function RegUser() {
            $.ajax({
                type: "POST",//方法
                url: "../server/RegUser.php" ,//表单接收url
                data: $('#form1').serialize(),
                success: function (data) {
                  //提交成功
                    var result=document.getElementById("Result");
                    result.innerHTML=data[0].result;
                    //注册后跳转
                    if (data[0].result == "注册成功") {
                        location.href="LoginFrom.html";
                    }
                },
                error : function(data) {
                  //提交失败
                    var result=document.getElementById("Result");
                    result.innerHTML="连接服务器失败";
                }
            });
        }
    </script>
</html>

RegUser.php

<?php
header("Content-type:application/json");
// 注册账号验证

$username = $_POST["username"];//获取表单POST过来的用户名
$password = $_POST["password"];//获取表单POST过来的密码
$cpassword = $_POST["cpassword"];//获取表单POST过来的重复密码

//表单过滤

$user = trim($username);//过滤空格
$psw = trim($password);//过滤空格
$cpsw = trim($cpassword);//过滤空格

//开始判断

if($user == "" && $psw == "" && $cpsw == ""){
    echo "[{\"result\":\"都还没填呢!\"}]";
}else if($user == "" && $psw == ""){
    echo "[{\"result\":\"账号和密码都还没填呢\"}]";
}else if ($psw == "" && $cpsw == "") {
    echo "[{\"result\":\"密码都还没填呢\"}]";
}else if($user == "" && $cpsw == ""){
    echo "[{\"result\":\"账号和重复密码还没填呢\"}]";
}else if ($user == "") {
    echo "[{\"result\":\"账号还没填呢\"}]";
}else if ($psw == "") {
    echo "[{\"result\":\"密码还没填呢\"}]";
}else if ($cpsw == "") {
    echo "[{\"result\":\"重复密码还没填呢\"}]";
}else if ($psw !== $cpsw) {
    echo "[{\"result\":\"两次输入密码不一样呢\"}]";
}else if (strlen($user) < 5){
    echo "[{\"result\":\"账号不能小于5位数\"}]";
}else if (strlen($psw) < 8){
    echo "[{\"result\":\"密码不能小于8位数\"}]";
}else if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $user)>0){
    echo "[{\"result\":\"账号不能为中文\"}]";
}else if (preg_match('/[\x{4e00}-\x{9fa5}]/u', $user)>0){
    echo "[{\"result\":\"账号不能存在中文\"}]";
}else if(preg_match("/[\'.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$user)){
    echo "[{\"result\":\"账号不能存在特殊符号\"}]";
}else{
    //生成用户ID,为简单学习,随机生成随机数
    $uid = rand(10000,99999);
    //密码MD5加密
    $md5psw = MD5($psw);
    // 数据库连接
    $con = mysql_connect("localhost","root","root");
    mysql_select_db("test", $con);
    mysql_query("SET NAMES UTF8");

    //查询数据库是否有存在该用户
    $exist = mysql_query("SELECT * FROM userlist WHERE username = '$user'");
    $exist_result = mysql_num_rows($exist);
    if($exist_result){
        //如果存在该用户
        echo "[{\"result\":\"该账号已被注册\"}]";
    }else{
        //插入数据库
        mysql_query("INSERT INTO userlist (username, password, userid) VALUES ('$user', '$md5psw', '$uid')");
        echo "[{\"result\":\"注册成功\"}]";
    }
}
?>

登陆

LoginFrom.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户登陆</title>
    <link rel="stylesheet" type="text/css" href="css/Login-Reg.css">
</head>
<body>
    <h2 id="Reg-text">用户登陆</h2>
    <div id="Form-con">
    <form id="form1" onsubmit="return false" action="##" method="post">
        <input type="text" name="username" class="inputStyle" placeholder="请输入账号"><br/>
        <input type="password" name="password" class="inputStyle" placeholder="请输入密码"><br/>
        <input type="submit" class="btnStyle" value="登陆" onclick="LoginUser()">
    </div>
    <div id="Result"></div>
    </form>
</body>
<!-- AJAX提交表单 -->
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript">
        function LoginUser() {
            $.ajax({
                type: "POST",//方法
                url: "../server/LoginUser.php" ,//表单接收url
                data: $('#form1').serialize(),
                success: function (data) {
                    var result=document.getElementById("Result");
                    //提交成功
                    result.innerHTML=data[0].result;
                    if (data[0].result == "登陆成功") {
                        result.innerHTML="登陆成功";
                    }else if (data[0].result == "fail") {
                        result.innerHTML="登陆失败,账号或密码错误";
                    }
                },
                error : function(data) {
                  //提交失败的提示词或者其他反馈代码
                    var result=document.getElementById("Result");
                    result.innerHTML="连接服务器失败";
                }
            });
        }
    </script>
</html>

LoginUser.php

<?php
header("Content-type:application/json");
// 注册账号验证

$username = $_POST["username"];//获取表单POST过来的用户名
$password = $_POST["password"];//获取表单POST过来的密码

//表单过滤

$user = trim($username);//过滤空格
$psw = trim($password);//过滤空格

//开始判断

if($user == "" && $psw == ""){
    echo "[{\"result\":\"账号和密码都没填呢!\"}]";
}else if ($user == "") {
    echo "[{\"result\":\"账号还没填呢\"}]";
}else if ($psw == "") {
    echo "[{\"result\":\"密码还没填呢\"}]";
}else{
    // 数据库连接
    $con = mysql_connect("localhost","root","root");
    mysql_select_db("test", $con);
    mysql_query("SET NAMES UTF8");

    //校验MD5密码
    $md5psw = MD5($psw);

    //查询数据库账号密码是否一致
    $exist = mysql_query("SELECT * FROM userlist WHERE username = '$user' AND password = '$md5psw'");
    $exist_result = mysql_num_rows($exist);
    if($exist_result){
        echo "[{\"result\":\"登陆成功\"}]";
    }else{
        echo "[{\"result\":\"fail\"}]";
    }
}
?>

数据库

自增id –id –int(11)
用户id –userid — varchar(32)
用户名 –username — varchar(32)
密码 –password — varchar(32)
注册时间 –regtime –timestamp

demo

http://www.likeyunba.com/demo…

Author:TANKING
web:http://likeyunba.com
WeChat:face6009
Date:2019-2-2

    原文作者:TANKING
    原文地址: https://segmentfault.com/a/1190000018094149
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞