<!-- login.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用户登录系统</title>
<style>
* {
padding: 0;
margin: 0;
list-style: none;
}
fieldset{
width: 280px;
height: 150px;
margin: 36px auto;
padding: 24px;
}
.login_btn {
width: 72px;
margin: 12px 96px;
}
input {
margin: 5px auto;
}
</style>
</head>
<body>
<form action="login.php" method="post">
<fieldset>
<legend>用户登录</legend>
<ul>
<li>
<label for="">用户名:</label>
<input type="text" name='username'>
</li>
<li>
<label for="">密 码:</label>
<input type="password" name='pwd'>
</li>
<li>
<label for=""> </label>
<input type="checkbox" name='remember' value='yes'> 7天内自动登录
</li>
<li>
<label for=""> </label>
<input type="submit" name='login' value="登录" class='login_btn'>
</li>
</ul>
</fieldset>
</form>
</body>
</html>
# login.php
<?php
header("Content-Type: text/html; charset=utf-8");
session_start();
if(isset($_POST['login'])){
$username= trim($_POST['username']);
$password= trim($_POST['pwd']);
if(($username== '') || ($password== '')){
header('refresh: 3; url= login.html');
echo '该用户名或者密码不能为空,3秒后跳转到登录页面!';
exit;
}elseif(($username!= 'Sunny') || ($password!= 'password')){
header('refresh: 3; url= login.html');
echo '用户名或者密码错误,3秒后跳转到登录页面!';
exit;
}elseif(($username== 'Sunny') || ($password== 'password')){
$_SESSION['username']= $username;
$_SESSION['islogin']= 1;
if($_POST['remember']=='yes'){
setcookie("username", $username, time()+ 7*24*60*60);
setcookie("code", md5($username.md5($password)), time()+ 7*24*60*60);
}else{
setcookie("username", '', time()-1);
setcookie("code", '', time()-1);
}
header("location: index.php");
}
}
?>
# index.php
<?php
header("Content-Type: text/html; charset=utf-8");
session_start();
if(isset($_COOKIE['username'])){
$_SESSION['username']= $_COOKIE['username'];
$_SESSION['islogin']= 1;
}
if(isset($_SESSION['islogin'])){
echo "{$_SESSION['username']}您好,欢迎来到个人中心!<br>";
echo "<a href='logout.php'>注销</a>";
}else{
echo "您还未登录,请<a href='login.html'>登录</a>";
}
?>
# logout.php
<?php
header("Content-Type: text/html; charset=utf-8");
session_start();
$username= $_SESSION['username'];
$_SESSION=array();
session_destroy();
setcookie("username", '', time()-1);
setcookie("code", '', time()-1);
echo "{$username},欢迎您下次再来!";
echo "重新<a href='login.html'>登录</a>";
?>