php学习-实战项目

登录注册

login_db_connect.php

连接数据库

<?php
//用于登录界面数据库连接
//设置字符集
header('Content-type:text/html;charset=utf8');

//连接数据库
$con=mysqli_connect("localhost","root","root","lms");
if (mysqli_connect_errno($con))
{
    echo "连接 MySQL 失败: " . mysqli_connect_error();
} 

login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录</title>
<link href=../css/login.css rel="stylesheet"/>
</head>

<body background="./images/loginbg.png" style="background-size: 100% 100%;background-attachment: fixed;">
<div class=box>
	<div class=title>用户登录</div>
	<form action="../login.php" method="post">
		<table class=login>
			<tr><th>用户名:</th><td><input type="text" name="username"/></td></tr>
			<tr><th>密码:</th><td><input type="password" name="password"/></td></tr>
			<tr><th></th><td><input type="submit" value="登录"/><a href="register.php"><input type="button" value='前往注册'></a></td>
						
			</tr>
			
		</table>
	
	</form>
</div>
</body>
</html>

login.php

<?php
header('content-type:text/html;charset=utf-8');
//登录界面
require 'login_db_connect.php';//连接数据库


//判断表单是否提交,用户名密码是否提交
if (isset($_POST['username'])&&isset($_POST['password'])){//登录表单已提交
    

    //获取用户输入的用户名密码
    $username=$_POST["username"];
    $pwd=$_POST["password"];
    $sql="select id,username,password from user where username='$username' and password='$pwd';";
    $result=mysqli_query($con, $sql);//执行sql语句
    $row=mysqli_num_rows($result);//返回值条目
    if (!$row){//若返回条目不存在则证明该账号不存在或者密码输入错误
        echo "<script>alert('账号不存在或密码错误,点击前往注册');location='./register.php'</script>";
        //exit('账号或密码错误');
    }else{//存在返回条目证明用户账号密码匹配,进入主页面
        session_start();
        $_SESSION['username']=$_POST['username'];
        echo "<script>alert('欢迎');location='./index.php'</script>";
    }   
}


require './view/login.html';

register.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户注册</title>
<link href=../css/login.css rel="stylesheet"/>
</head>

<body background="./images/loginbg.png" style="background-size: 100% 100%;background-attachment: fixed;">
<div class=box>
	<div class=title>用户注册</div>
	<form action="../register.php" method="post">
		<table class=login>
			<tr><th>用户名:</th><td><input type="text" name="username"/></td></tr>
			<tr><th>密码:</th><td><input type="password" name="pwd"/></td></tr>
			<tr><th></th><td><input type="submit" value="注册"/></td>
			</tr>
			
		</table>
	
	</form>
</div>
</body>
</html>

register.php

<?php
header('content-type:text/html;charset=utf-8');
//注册页面
require 'login_db_connect.php';//连接数据库

//判断表单是否提交,用户名密码是否提交
if (isset($_POST['username'])&&isset($_POST['pwd'])){//登录表单已提交
    //获取用户输入的用户名密码
    $user=$_POST["username"];
    $pwd=$_POST["pwd"];
    //判断提交账号密码是否为空
    if ($user=='' || $pwd==''){
        exit('账号或密码不能为空');
    }else {
        $sql="insert into user(username,password) values ('$user','$pwd');";//添加账户sql语句
        $select="select username from user where username='$user'";
        $result=mysqli_query($con, $select);//执行sql语句
        $row=mysqli_num_rows($result);//返回记录数
        if(!$row){//记录数不存在则说明该账户没有被注册过
            if (mysqli_query($con,$sql)){//查询insert语句是否成功执行,成功将返回 TRUE。如果失败,则返回 FALSE。
                //跳转登录页面
                echo "<script>alert('注册成功,请登录');location='./login.php'</script>";
            }else{//失败则重新跳转注册页面
                echo "<script>alert('注册失败,请重新注册');location='./regsiter.php'</script>";
            }
        }else{//存在记录数则说明注册的用户已存在
            echo "<script>alert('该用户已经存在,请直接登录');location='./login.php'</script>";
        }
    }  
}


require './view/register.html';

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值