连接数据库
<?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();
}
<!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>
<?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';
<!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>
<?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';