<?php
//路径:0508-login-handle
setcookie('a','admin','/');
//setcookie('a','admin',time()-60,'/');
echo $_COOKIE['a'];
session_start();
$_SESSION['password']=sha1(md5('123').'php.cn123');
session_destroy();//id还在
setcookie('PHPSESSID',NULL,time()-60,'/');
if(isset($_COOKIE['user'])){
$user=unserialize($_COOKIE['user']);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device_width,initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="./0508.css">
<title>首页</title>
</head>
<body>
<nav>
<a href="">我的论坛</a>
<a href="login.php">登录</a>
</nav>
</body>
</html>
@CHARSET "UTF-8";
nav{
height:40px;
background-color:deepskyblue;
padding:0 20px;
display:flex;
justify-content:space-between;
align-items:center;
}
nav>a{
color:white;
text-decoration:none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device_width,initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="./0508.css">
<title>用户登录</title>
</head>
<body>
<h3>用户登录</h3>
<form action="handle.php?action=login" method="post">
<div>
<label for="email">邮箱:</label>
<input type="email" name="email" id="email" placeholder="demo@email.com" require autofocus>
</div>
<div>
<label for="password">密码:</label>
<input type="password" name="password" id="password" placeholder="不少于6位" required>
</div>
<div>
<button>提交</button>
</div>
</form>
</body>
<script type="text/javascript">
//验证两次密码是否相同
//注意:这个js不是这里用的,是注册页面用的,
//p1/p2是两个input,tips是一个span的id;
function compare(){
if(document.foms[0].p1.value.trim()!==document.forms[0].p2.value.trim()){
document.querySelector('#tips').innerText='二次密码不相同';
return false;
}
}
</script>
</html>
<?php
$pdo=new PDO('mysql:host=localhost;dbname=php7','root','');
$stmt=$pdo->prepare('SELECT * FROM `t_user`');
$stmt->execute();
$user=$stmt->fetchAll(PDO::FETCH_ASSOC);
$action=$_GET['action'];
switch (strtolower($action)){
case 'login':
if($_SERVER['REQUEST_METHOD']==='POST'){
$email=$_POST['email'];
$password=sha1($_POST['password']);
$result=array_filter($user,function ($value)use($email,$password){
return $value['email']===$email && $value['password']===$password;
});
if(count($result)===1){
setcookie('user',serialize(array_pop($result)));
exit('<script>alert("验证通过");location.href="0508.php"</script>');
}else {
exit('<script>alert("邮箱或密码错误");location.href="login.php"</script>');
}
}else {
die('请求类型错误');
}
break;
default:
exit('未定义操作');
}