login.html
<html>
<head>
<title>用户登录</title>
<meta charset="utf-8">
</head>
<body>
<title>请输入。。。</title>
<form action="log.php?action=login" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password" ><br>
<input type="submit" value="登陆">
<input type="reset" value="清空">
</form>
</body>
</html>
log.php
<?php
function clearCookies(){
setcookie('username','',time()-3600);
setcookie('username','',time()-3600);
}
if ($_GET["action"]=="login") {
clearCookies();
if ($_POST["username"]=="admin")&&$_POST["password"]=="123456"){
setcookie('username',$_POST["username"],time()+60*60*24*7);
setcookie('password',$_POST["password"],time()+60*60*24*7);
header("Localtion:text.php");
}else{
die("输入错误TAT,请重新输入~");}
}else if($_GET["action"]=="logout"){
clearCookies();
}
?>
text.php
<?php
if(!(isset($_COOKIE['isLogin'])&&$_COOKIE['isLogin']=='1')){
header("Location:log.php");exit;
}
?>
<html>
<head>
<title>我的主页</title>
<meta charset="utf-8">
</head>
<body>
<?php
echo "Hi".$_COOKIE["username"];
?>
欢迎进入~
<a href="log.php? action=logout">退出</a>
</body>
</html>
子期不遇