首页登录入口
<?php
namespace _0825;
session_start();
// 判断是否已登录?
if (isset($_SESSION['user'])) {
$us = unserialize($_SESSION['user']);
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>首页登录入口</title>
</head>
<style>
nav {
height: 40px;
background-color: deepskyblue;
padding: 0 20px;
display: flex;
justify-content: space-between;
align-items: center;
}
nav>a {
color: white;
text-decoration: none;
}
</style>
<body>
<nav>
<a href="index.php">我的博客</a>
<?php if (isset($us)) : ?>
<span style="margin-left: 400px">用户名:<?= $us['name'] ?></span>
<span style="color: red;">邮箱:<?= $us['email'] ?></span>
<a id="logout">退出</a>
<?php else : ?>
<a href="login.php">登录</a>
<?php endif ?>
</nav>
<script>
document.querySelector('#logout').addEventListener('click', function(event) {
if (confirm('是否退出')) {
event.preventDefault();
window.location.assign("top.php?us=logout");
}
});
</script>
</body>
</html>
用户登录login.php
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用户登录</title>
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
<style type="text/css">
body,
html {
font-size: 100%;
padding: 0;
margin: 0;
}
/* Reset */
*,
*:after,
*:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
body {
background: #00b4ef;
color: #D5D6E2;
font-weight: 500;
font-size: 1.05em;
font-family: "Microsoft YaHei", "Segoe UI", "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: rgba(255, 255, 255, 0.6);
outline: none;
text-decoration: none;
-webkit-transition: 0.2s;
transition: 0.2s;
}
a:hover,
a:focus {
color: #74777b;
text-decoration: none;
}
.form-horizontal {
background: #fff;
padding-bottom: 40px;
border-radius: 15px;
text-align: center;
}
.form-horizontal .heading {
display: block;
font-size: 35px;
font-weight: 700;
padding: 35px 0;
border-bottom: 1px solid #f0f0f0;
margin-bottom: 30px;
}
.form-horizontal .form-group {
padding: 0 40px;
margin: 0 0 25px 0;
position: relative;
}
.form-horizontal .form-control {
background: #f0f0f0;
border: none;
border-radius: 20px;
box-shadow: none;
padding: 0 20px 0 45px;
height: 40px;
transition: all 0.3s ease 0s;
}
.form-horizontal .form-control:focus {
background: #e0e0e0;
box-shadow: none;
outline: 0 none;
}
.form-horizontal .form-group i {
position: absolute;
top: 12px;
left: 60px;
font-size: 17px;
color: #c8c8c8;
transition: all 0.5s ease 0s;
}
.form-horizontal .form-control:focus+i {
color: #00b4ef;
}
.form-horizontal .fa-question-circle {
display: inline-block;
position: absolute;
top: 12px;
right: 60px;
font-size: 20px;
color: #808080;
transition: all 0.5s ease 0s;
}
.form-horizontal .fa-question-circle:hover {
color: #000;
}
.form-horizontal .main-checkbox {
float: left;
width: 20px;
height: 20px;
background: #11a3fc;
border-radius: 50%;
position: relative;
margin: 5px 0 0 5px;
border: 1px solid #11a3fc;
}
.form-horizontal .main-checkbox label {
width: 20px;
height: 20px;
position: absolute;
top: 0;
left: 0;
cursor: pointer;
}
.form-horizontal .main-checkbox label:after {
content: "";
width: 10px;
height: 5px;
position: absolute;
top: 5px;
left: 4px;
border: 3px solid #fff;
border-top: none;
border-right: none;
background: transparent;
opacity: 0;
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
}
.form-horizontal .main-checkbox input[type=checkbox] {
visibility: hidden;
}
.form-horizontal .main-checkbox input[type=checkbox]:checked+label:after {
opacity: 1;
}
.form-horizontal .text {
float: left;
margin-left: 7px;
line-height: 20px;
padding-top: 5px;
text-transform: capitalize;
}
.form-horizontal .text a {
color: #000;
}
.form-horizontal .text a:hover {
color: red;
}
.form-horizontal .btn {
float: right;
font-size: 14px;
color: #fff;
background: #00b4ef;
border-radius: 30px;
padding: 10px 25px;
border: none;
text-transform: capitalize;
transition: all 0.5s ease 0s;
}
@media only screen and (max-width: 479px) {
.form-horizontal .form-group {
padding: 0 25px;
}
.form-horizontal .form-group i {
left: 45px;
}
.form-horizontal .btn {
padding: 10px 20px;
}
}
</style>
</head>
<body>
<div class="demo" style="padding: 20px 0;">
<div class="container">
<div class="row">
<div class="col-md-offset-3 col-md-6">
<form class="form-horizontal" action="top.php?us=login" method="post" onsubmit="return compare()">
<span class="heading">用户登录</span>
<div class="form-group">
<input type="email" class="form-control" name="email" id="inputEmail" placeholder="请输入邮箱">
<i class="fa fa-user"></i>
</div>
<div class="form-group help">
<input type="password" class="form-control" name="password" id="Password" placeholder="请输入密码">
<i class="fa fa-lock"></i>
</div>
<div class="form-group">
<span class="text"><a href="reg.php">注册用户</a></span>
<button type="submit" class="btn btn-default">登录</button>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
注册用户reg.php
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>注册用户</title>
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
<style type="text/css">
body,
html {
font-size: 100%;
padding: 0;
margin: 0;
}
/* Reset */
*,
*:after,
*:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
body {
background: #00b4ef;
color: #D5D6E2;
font-weight: 500;
font-size: 1.05em;
font-family: "Microsoft YaHei", "Segoe UI", "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: rgba(255, 255, 255, 0.6);
outline: none;
text-decoration: none;
-webkit-transition: 0.2s;
transition: 0.2s;
}
a:hover,
a:focus {
color: #74777b;
text-decoration: none;
}
.form-horizontal {
background: #fff;
padding-bottom: 40px;
border-radius: 15px;
text-align: center;
}
.form-horizontal .heading {
display: block;
font-size: 35px;
font-weight: 700;
padding: 35px 0;
border-bottom: 1px solid #f0f0f0;
margin-bottom: 30px;
}
.form-horizontal .form-group {
padding: 0 40px;
margin: 0 0 25px 0;
position: relative;
}
.form-horizontal .form-control {
background: #f0f0f0;
border: none;
border-radius: 20px;
box-shadow: none;
padding: 0 20px 0 45px;
height: 40px;
transition: all 0.3s ease 0s;
}
.form-horizontal .form-control:focus {
background: #e0e0e0;
box-shadow: none;
outline: 0 none;
}
.form-horizontal .form-group i {
position: absolute;
top: 12px;
left: 60px;
font-size: 17px;
color: #c8c8c8;
transition: all 0.5s ease 0s;
}
.form-horizontal .form-control:focus+i {
color: #00b4ef;
}
.form-horizontal .fa-question-circle {
display: inline-block;
position: absolute;
top: 12px;
right: 60px;
font-size: 20px;
color: #808080;
transition: all 0.5s ease 0s;
}
.form-horizontal .fa-question-circle:hover {
color: #000;
}
.form-horizontal .main-checkbox {
float: left;
width: 20px;
height: 20px;
background: #11a3fc;
border-radius: 50%;
position: relative;
margin: 5px 0 0 5px;
border: 1px solid #11a3fc;
}
.form-horizontal .main-checkbox label {
width: 20px;
height: 20px;
position: absolute;
top: 0;
left: 0;
cursor: pointer;
}
.form-horizontal .main-checkbox label:after {
content: "";
width: 10px;
height: 5px;
position: absolute;
top: 5px;
left: 4px;
border: 3px solid #fff;
border-top: none;
border-right: none;
background: transparent;
opacity: 0;
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
}
.form-horizontal .main-checkbox input[type=checkbox] {
visibility: hidden;
}
.form-horizontal .main-checkbox input[type=checkbox]:checked+label:after {
opacity: 1;
}
.form-horizontal .text {
float: left;
margin-left: 7px;
line-height: 20px;
padding-top: 5px;
text-transform: capitalize;
}
.form-horizontal .text a {
color: #000;
}
.form-horizontal .text a:hover {
color: red;
}
.form-horizontal .btn {
float: right;
font-size: 14px;
color: #fff;
background: #00b4ef;
border-radius: 30px;
padding: 10px 25px;
border: none;
text-transform: capitalize;
transition: all 0.5s ease 0s;
}
@media only screen and (max-width: 479px) {
.form-horizontal .form-group {
padding: 0 25px;
}
.form-horizontal .form-group i {
left: 45px;
}
.form-horizontal .btn {
padding: 10px 20px;
}
}
</style>
</head>
<body>
<div class="demo" style="padding: 20px 0;">
<div class="container">
<div class="row">
<div class="col-md-offset-3 col-md-6">
<form class="form-horizontal" action="top.php?us=reg" method="post" onsubmit="return compare()">
<span class="heading">注册用户</span>
<div class="form-group">
<input type="text" class="form-control" name="name" id="inputName" placeholder="请输入昵称">
<i class="fa fa-user"></i>
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" id="inputEmail" placeholder="请输入邮箱">
<i class="fa fa-user"></i>
</div>
<div class="form-group help">
<input type="password" class="form-control" name="password1" id="Pass1" placeholder="密码不少于8位">
<i class="fa fa-lock"></i>
</div>
<div class="form-group help">
<input type="password" class="form-control" name="password2" id="Pass2" placeholder="重复输入密码">
<i class="fa fa-lock"></i>
</div>
<div class="form-group">
<span class="text"><a href="login.php">用户登录</a></span>
<button type="submit" class="btn btn-default" id="reg">提交注册</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
function password() {
if (document.forms[0].pass1.value.trim() !== document.forms[0].pass2.value.trim()) {
document.querySelector('#reg').innerText = "两次密码不一样,请重新设置。";
return false;
}
}
</script>
</body>
</html>
会话处理top.php
<?php
namespace _0825;
use PDO;
// 开户会话
session_start();
// 连接数据库
$db = new PDO('mysql:dbname=phpedu', 'root', '4889671');
$stmt = $db->prepare('SELECT * FROM `admin_user`;');
if ($stmt->execute()) {
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else {
print_r($stmt->errorInfo());
}
// 获取用户
$us = strtolower($_GET['us']);
switch ($us) {
// 注册
case 'reg':
// 1. 获取新用户的数据
$name = $_POST['name'];
$email = $_POST['email'];
$password = sha1(md5($_POST['password2']));
$datetime = time();
$sql = "INSERT `admin_user` SET `name`= ?, `email`= ?, `password`= ?, `datetime`= ?";
$stmt = $db->prepare($sql);
$data = [$name, $email, $password, $datetime];
if ($stmt->execute($data)) {
if ($stmt->rowCount() > 0) {
// 注册成功之后,让用户自动登录
$sql = 'SELECT * FROM `admin_user` WHERE `id` = ' . $db->lastInsertId();
$stmt = $db->prepare($sql);
$stmt->execute();
$newUser = $stmt->fetch(PDO::FETCH_ASSOC);
$_SESSION['user'] = serialize($newUser);
exit('<script>alert("注册成功");location.href="index.php"</script>');
} else {
exit('<script>alert("注册失败");location.href="reg.php"</script>');
}
} else {
print_r($stmt->errorInfo());
}
// 登录
case 'login':
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'];
$password = sha1(md5($_POST['password']));
$result = array_filter($users, function ($user) use ($email, $password) {
return $user['email'] === $email && $user['password'] === $password;
});
if (count($result) === 1) {
// 登录成功,写入session
$_SESSION['user'] = serialize(array_pop($result));
exit('<script>alert("验证通过");location.href="index.php"</script>');
}
exit('请求类型错误');
}
case 'logout':
if (isset($_SESSION['user'])) {
session_destroy();
exit('<script>alert("退出成功");location.href="index.php"</script>');
}
default:
exit('参数非法或未定义操作');
}
运行效果