案例-----用PHP进行注册登录验证

用php进行注册登录验证,首先要理清思路,需要写哪些html页面和php执行程序,这些页面分别要承担哪些功能。

服务器:Apache
数据库:MySQL

思路:
1.注册页面register.html
获取用户名和密码,通过form表单提交到后台,准备存入数据库

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>注册页面</title>
</head>
<body>
    <form method="post" action="register.php" id="form2">
        <p>用户名:<input name="uname" type="text" id="uname"/></p>
        <p>密  码:<input name="upwd" type="password" id="upwd"/></p>
        <input type="submit" value="提交" id="btn">
    </form>
</body>
</html>

2.注册后台处理程序 register.php
将从前端获取来的用户名和密码存入数据库中,以便登录时进行验证,插入成功后自动跳转到登录页面 login.html

<?php
    //防止中文乱码,针对整个php文件;
    header("content-type:text/html;charset=utf-8");
    //后端接收用户名和密码
    $uname = $_POST["uname"];
    $upwd = $_POST["upwd"];
    //连接数据库(主机名或IP地址,用户名,密码)
    mysql_connect("127.0.0.1","root","root");
    //选择数据库
    mysql_select_db("1127");
    //设置字符集(针对数据库)  myqsl_query()让括号里面的sql语句执行,sql语句必须加引号
    mysql_query("set names utf8");
    //在sql语句中,如果你的变量是字符串,必须加引号
    $sql = "select * from user where uname='$uname'";
    // echo $sql;
    //得到资源
    $res = mysql_query($sql);
    //要把资源变成数组,如果资源为空,那么转为数组也为空
    $arr = mysql_fetch_assoc($res);
    //print_r($arr);
    if($arr){//为真,即不为空,说明存在,不能注册
        echo "<script>alert('用户名已存在,请重新注册');window.location.href='register.html'</script>";
    }else{//
        $sql = "insert into user (uname,upwd) values ('$uname','$upwd')";
        $res = mysql_query($sql);  //如果添加成功返回true
        if($res){//添加成功,为真
            echo "<script>alert('注册成功');window.location.href='login.html'</script>";
        }else{
            echo "<script>alert('注册失败')</script>";
        }
    }
?>

3.登录页面 login.html
输入用户名和密码提交到后台,进行验证

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>登录页面</title>
</head>
<body>
    <form method="post" action="login.php" id="from1">
        <p>用户名:<input name="uname" type="text" id="username"/></p>
        <p>密  码:<input name="upwd" type="password" id="userpwd"/></p>
        <input type="submit" value="登录" id="btn"/>
    </form>
</body>
</html>

4.登录后台处理程序 login.php
将从登录页面获取来的用户名和密码和数据库中的数据进行比较,如果存在,即登录成功,跳转到首页 index.html

<?php
    header("content-type:text/html;charset=utf-8");
    $username = $_POST["uname"];
    $userpwd = $_POST["upwd"];
    mysql_connect("127.0.0.1","root","root");
    mysql_select_db("1127");
    mysql_query("set names utf8");
    $sql = "select * from user where uname='$username'";
    $res = mysql_query($sql);
    $arr = mysql_fetch_assoc($res);
    //print_r($arr);
    if($arr){  //如果存在该用户名
        if($userpwd == $arr["upwd"]){ //如果密码正确,判断后台获取到的密码和数据库里的密码是否一致
            echo "<script>window.location.href='../last/index.php'</script>";
        }else{
            echo "<script>alert('密码不正确');window.location.href='index.html'</script>";
        }
    }else{
        echo "<script>alert('用户名不正确,请重新输入');window.location.href='login.html'</script>";
    }
?>

如有错误,欢迎各位大佬留言指点一二,万分感激!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值