本代码实现了php+Mysql数据库 用户的注册和登录功能,希望对大家有帮助,后附加源码,注释详细~
first要贴上表结构~
登录页面
前台登录页面denglu.html
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录页面</title>
<script type="text/javascript">
//检查用户名是否填写
function checkName(){
var name = document.getElementById("add_name").value;
var spanNode = document.getElementById("user_name");
if (name != "") {
spanNode.innerHTML = "已填".fontcolor("green");
return true;
}else{
spanNode.innerHTML = "不能为空".fontcolor("red");
return false;
}
}
//检查密码是否填写
function checkPassword(){
var password = document.getElementById("add_password").value;
var spanNode = document.getElementById("user_password");
if (password != "") {
spanNode.innerHTML = "已填".fontcolor("green");
return true;
}else{
spanNode.innerHTML = "请输入密码".fontcolor("red");
return false;
}
}
//点击登录时进行检查
function checkForm(){
var name = checkName();
var password = checkPassword();
if (name && password) {
return true;
}else{
return false;
}
}
</script>
</head>
<style type="text/css">
.wrapper{
text-align: center;
width:1000px;
margin:20px auto;
}
h2{
background-color:#7CCD7C;
margin:0px;
text-align:center;
}
.add{
margin:20px auto;
}
.add labal{
text-align: center;
background-color: #FFB6C1;
color: #fff;
margin:20px auto;
}
.btn {
background-color: #008CBA;;
border-radius:8px;
color: white;
padding: 6px 93px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 14px;
}
.error {
color: #FF0000;
}
</style>
<body>
<div class="wrapper">
<h2>用户管理系统</h2>
<div class="add">
<labal>登录系统</labal>
</div>
<div class="add">
<form method="post" action="denglu.php" onsubmit="return checkForm()">
<labal>用户名:</labal> <input type="text" name="add_name" id="add_name" placeholder="请输入用户名">
<span id="user_name" class="error">*</span><br><br>
<labal>密码:</labal>    <input type="password" name="add_password" id="add_password"placeholder="请输入密码">
<span id="user_password" class="error">*</span><br><br>
<input class="btn" type="submit" value="登录">
<br>
</form>
<a href="zhuce.html">立即注册</a>
</div>
</div>
</body>
</html>
后台处理页面代码denglu.php
代码
<?php
require("./MysqlData.php");
$user_name = $_POST['add_name'];
$user_password = $_POST['add_password'];
$password = md5($user_password);
$data = new MysqlData;
$sql = $data->select_user($user_name,$password);
$res = $data->sqlRun($sql);
//若账号密码错误
if ($res->num_rows == 0) {
//释放$res占用的内存
mysqli_free_result($res);
//关闭数据库连接
$data->closeConn();
//提示输入正确的账号
echo "<script type='text/javascript'>";
echo "alert('账号密码错误,请重新输入');";
echo "history.back();";
echo "</script>";
}
//若账号密码正确
else{
//获取id字段
$id = mysqli_fetch_object($res)->id;
//释放$res占用的内存
mysqli_free_result($res);
//关闭数据库连接
$data->closeConn();
//将用户数据加入cookies
setcookie("id",$id);
setcookie("password","TRUE");
header("Location:main.php");
}
$data->closeConn();
?>
登录跳转页面main.php
代码
<?php
require("./MysqlData.php");
/*检查cookie中的password变量是否不等于true,是的话表示尚未登录网站,就重定向到denglu.html页面*/
$id = $_COOKIE["id"];
$password = $_COOKIE["password"];
if ($password != "TRUE") {
header("location:denglu.html");
exit();
}
//创建对象
$data = new MysqlData;
$sql = "SELECT * FROM users WHERE id = ".$id.";";
$row = $data->getRow($sql);
// echo $sql;
$data->closeConn();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录成功页面</title>
</head>
<style type="text/css">
.wrapper{
text-align: center;
width:1000px;
margin:20px auto;
}
h2{
background-color:#7CCD7C;
margin:0px;
text-align:center;
}
.my{
margin:20px auto;
}
.my labal{
text-align: center;
background-color: #FFB6C1;
color: #fff;
margin:20px auto;