您当前的位置:首页 > php > cookie的应用

cookie的应用

日期:2020-11-07 10:11:00    浏览:195

index.html文件内容

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>登录页面</title>
<style>
body{
text-align: center;
}
</style>
</head>
<body>
<h1>登录页面</h1>
<form action="do_login.php" method="post">
用户名:
<input type="text" name="username"><br>
密码:
<input type="password" name="password"><br>
<input type="submit" name="submit" value="登录">
</form>
</body>
</html>

do_login.php内容

<?php
if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=$_POST['password'];
if($username == "xiao" && $password == "123"){
setcookie('islogin',true,time() + 3600,'/');
setcookie('username',$username,time() + 3600,'/');
setcookie('sex','girl',time() + 3600,'/');
echo '<h3>登录成功</h3>' . $username;
echo "<meta http-equiv='refresh' content='3;url=finsh.php'>";
}else{
echo "<font size='4' color='red'>您还未登录</font>";
}
}

finsh.php内容

<?php
var_dump($_COOKIE);
echo "登录成功" . $_COOKIE['username'];
echo "<a href='logout.php'>退出登录</a>";

logout.php内容

<?php
echo "<font size='6' color='red'>这里是退出页面</font>";
var_dump($_COOKIE);
setcookie('islogin',null,time() - 1,'/');//后面的斜杠不要漏了,否则操作不成功
setcookie('username',null,time() - 1,'/');
setcookie('sex',null,time() - 1,'/');
echo "<a href='index.html'>进入登录</a>";

上一篇: 文件系统处理

下一篇: pdo事务

世事无常

要有遥不可及的梦想,也要有脚踏实地的本事