Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:验证的过程 , 一定要头脑清楚...其实这些都是标准件了, 有许多现成的模块可以用,但还是有必要了解实现的细节
setcookie(名称,值,[过期时间])
$_COOKIE['名称']
setcookie(名称,值,time()-1)
index.php
<?php include 'config.php'; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="css/public-header.css">
<link rel="stylesheet" href="css/public_style.css">
</head>
<body>
<!-- 头部 -->
<div class="public-header">
<!-- 左侧导航 -->
<div class="left-link">
<?php foreach($nav as $item): ?>
<a href="<?php echo $item['url']?>"><?php echo $item['name']; ?></a>
<?php endforeach; ?>
</div>
<!-- 左侧导航 end -->
<!-- 右侧按钮 -->
<div class="right-link">
<?php if(!isset($_COOKIE['user'])): ?>
<!-- 用户登录 -->
<a href="login.php">
<i class="iconfont icon-huiyuan2"></i>用户登录
</a>
<!-- 免费注册 -->
<a href="#">免费注册</a>
<?php else: ?>
<a href="#"><?php echo $_COOKIE['user']; ?></a>
<a href="handle.php?action=logout">退出登录</a>
<?php endif; ?>
</div>
<!-- 右侧按钮 end -->
</div>
<!-- 头部 end -->
</body>
</html>
login.php
<?php
if(filter_has_var(INPUT_COOKIE,'user')){
die('已登录,请勿重复登录');
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>用户登录</title>
<style>
form{
width: 400px;
height: 200px;
background-color: #0cbadf;
border-radius: 5px;
text-align: center;
margin: auto;
}
form > div{
display: grid;
grid-template-columns: 100px 200px;
margin: 20px 0;
grid-column-gap:10px;
}
form > div > label{
text-align: right;
}
form > div > input{
border-radius: 5px;
}
</style>
</head>
<body>
<form action="handle.php?action=login" method="post">
<h3>用户登录</h3>
<div>
<label for="username">用户名</label>
<input type="text" name="username" id="username" required autofocus>
</div>
<div>
<label for="password">密码</label>
<input type="password" name="password" id="password" required autofocus>
</div>
<input type="submit" value="登录">
</form>
</body>
</html>
handle.php
<?php
$user=[
['username'=>'admin','password'=>'21232f297a57a5a743894a0e4a801fc3']
];
$from_page=basename(filter_input(INPUT_SERVER,'HTTP_REFERER'));
$pages_arr=['index.php','login.php'];
if(!in_array($from_page,$pages_arr)){
die('非法来源');
}
$action=filter_input(INPUT_GET,'action',FILTER_SANITIZE_STRING);
switch($action){
case 'login':
$username=filter_input(INPUT_POST,'username',FILTER_SANITIZE_STRING);
$password=filter_input(INPUT_POST,'password',FILTER_SANITIZE_STRING);
if(!$username){
die('请输入用户名');
}
$i=0;
foreach($user as $item){
if(in_array($username,$item)){
$user_password=$item['password'];
break;
}else{
$i++;
}
}
if($i>=count($user)){
die('该用户未注册');
}
if(MD5($password)!=$user_password){
die('密码不正确');
}else{
setcookie('user',$username);
echo '<script>alert("登录成功!");window.location.href="index.php";</script>';
}
break;
case 'logout':
setcookie('user',$username,time()-1);
echo '<script>alert("退出登录成功!");window.location.href="index.php";</script>';
break;
}
session_start()
$_SESSION['名称']=值
$_SESSION['名称']
unset($_SESSION['名称'])
session_unset()
session_destory()
index.php
<?php
include 'config.php';
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="css/public-header.css">
<link rel="stylesheet" href="css/public_style.css">
</head>
<body>
<!-- 头部 -->
<div class="public-header">
<!-- 左侧导航 -->
<div class="left-link">
<?php foreach($nav as $item): ?>
<a href="<?php echo $item['url']?>"><?php echo $item['name']; ?></a>
<?php endforeach; ?>
</div>
<!-- 左侧导航 end -->
<!-- 右侧按钮 -->
<div class="right-link">
<?php if(!isset($_SESSION['user'])): ?>
<!-- 用户登录 -->
<a href="login.php">
<i class="iconfont icon-huiyuan2"></i>用户登录
</a>
<!-- 免费注册 -->
<a href="#">免费注册</a>
<?php else: ?>
<a href="#"><?php echo $_SESSION['user']; ?></a>
<a href="handle.php?action=logout">退出登录</a>
<?php endif; ?>
</div>
<!-- 右侧按钮 end -->
</div>
<!-- 头部 end -->
</body>
</html>
login.php
<?php
session_start();
if(isset($_SESSION['user'])){
die('已登录,请勿重复登录');
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>用户登录</title>
<style>
form{
width: 400px;
height: 200px;
background-color: #0cbadf;
border-radius: 5px;
text-align: center;
margin: auto;
}
form > div{
display: grid;
grid-template-columns: 100px 200px;
margin: 20px 0;
grid-column-gap:10px;
}
form > div > label{
text-align: right;
}
form > div > input{
border-radius: 5px;
}
</style>
</head>
<body>
<form action="handle.php?action=login" method="post">
<h3>用户登录</h3>
<div>
<label for="username">用户名</label>
<input type="text" name="username" id="username" required autofocus>
</div>
<div>
<label for="password">密码</label>
<input type="password" name="password" id="password" required autofocus>
</div>
<input type="submit" value="登录">
</form>
</body>
</html>
handle.php
<?php
session_start();
$user=[
['username'=>'admin','password'=>'21232f297a57a5a743894a0e4a801fc3']
];
$from_page=basename(filter_input(INPUT_SERVER,'HTTP_REFERER'));
$pages_arr=['index.php','login.php'];
if(!in_array($from_page,$pages_arr)){
die('非法来源');
}
$action=filter_input(INPUT_GET,'action',FILTER_SANITIZE_STRING);
switch($action){
case 'login':
$username=filter_input(INPUT_POST,'username',FILTER_SANITIZE_STRING);
$password=filter_input(INPUT_POST,'password',FILTER_SANITIZE_STRING);
if(!$username){
die('请输入用户名');
}
$i=0;
foreach($user as $item){
if(in_array($username,$item)){
$user_password=$item['password'];
break;
}else{
$i++;
}
}
if($i>=count($user)){
die('该用户未注册');
}
if(MD5($password)!=$user_password){
die('密码不正确');
}else{
$_SESSION['user']=$username;
echo '<script>alert("登录成功!");window.location.href="index.php";</script>';
}
break;
case 'logout':
unset($_SESSION['user']);
echo '<script>alert("退出登录成功!");window.location.href="index.php";</script>';
break;
}