<!-- 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>";
?>