逻辑图
index.php
<?php
session_start();
// var_dump($_SESSION);die;
// 判断一下是否登陆, 若是没有登陆, 跳转回登陆页
if(!isset($_COOKIE['username']) && !isset($_SESSION['username'])){
header('location:login.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>主页</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="container">
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand">
使用 Cookie 和 Session 实现会话控制
</a>
<a href="action.php?action=logout">
<button class="btn btn-outline-danger my-2 my-sm-0"
type="button">
注销
</button>
</a>
</nav>
<div class="d-flex justify-content-around mt-5">
<div class="card col-5">
<div class="card-body">
<h5 class="card-title">
会话控制实战内容一
</h5>
<h6 class="card-subtitle mb-2 text-muted">
SESSION 部分
</h6>
<p class="card-text">
实现用户认证功能,用户登陆、退出与身份识别
</p>
</div>
</div>
<div class="card col-5">
<div class="card-body">
<h5 class="card-title">
会话控制实战内容二
</h5>
<h6 class="card-subtitle mb-2 text-muted">
COOKIE 部分
</h6>
<p class="card-text">
实现登陆记住用户功能,七天免登陆认证
</p>
</div>
</div>
</div>
<div class="d-flex justify-content-around mt-4">
<div class="card col-5">
<div class="card-body">
<h5 class="card-title">
会话控制实战内容一
</h5>
<h6 class="card-subtitle mb-2 text-muted">
SESSION 部分
</h6>
<p class="card-text">
实现用户认证功能,用户登陆、退出与身份识别
</p>
</div>
</div>
<div class="card col-5">
<div class="card-body">
<h5 class="card-title">
会话控制实战内容二
</h5>
<h6 class="card-subtitle mb-2 text-muted">
COOKIE 部分
</h6>
<p class="card-text">
实现登陆记住用户功能,七天免登陆认证
</p>
</div>
</div>
</div>
<div class="d-flex justify-content-center mt-5">
<div class="alert alert-light" role="alert">
Powered by 云和数据
</div>
</div>
</div>
</body>
</html>
复制代码
login.php
<!DOCTYPE html>
<html>
<head>
<title>登录页</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="card col-12 mt-5">
<div class="card-body">
<h4 class="card-title">
用户登陆
</h4>
<div class="col-12 mt-4 d-flex justify-content-center">
<form class="col-8" method="post" action="action.php">
<input type="hidden" name="action" value="login">
<div class="form-group">
<label for="username">用户名</label>
<input type="text"
class="form-control"
id="username"
name="username"
placeholder="请输入用户名">
</div>
<div class="form-group">
<label for="password">密码</label>
<input type="password"
class="form-control"
id="password"
name="password"
placeholder="请输入密码">
</div>
<div class="form-group form-check">
<input type="checkbox"
class="form-check-input"
id="remember"
name="remember">
<label class="form-check-label"
for="remember">
在这台电脑上记住个人登陆状态
</label>
</div>
<button type="submit"
class="btn btn-primary">
登陆
</button>
</form>
</div>
</div>
</div>
<div class="d-flex justify-content-center mt-5">
<div class="alert alert-light" role="alert">
Powered by 云和数据
</div>
</div>
</div>
</body>
</html>
复制代码
action.php
<?php
session_start();
switch ($_REQUEST['action']) {
case 'login':
login();
break;
case 'logout':
logout();
}
function login(){
$username = $_POST['username'];
$password = $_POST['password'];
if (array_key_exists('remember', $_POST)) {
$remember = $_POST['remember'];
} else{
$remember = false;
}
if ($username !== 'yunhe' || $password !== "123456") {
echo "<script> alert('用户名或密码不正确!'); window.location='login.php'; </script>";
return;
}
if($remember){
setcookie('username',$username,time()+7*24*3600);
}
$_SESSION['username'] = $username;
header("location:index.php");
}
function logout(){
session_unset();
setcookie('username','',time()-1);
header('location:login.php');
}
复制代码