11-3. PHP基于cookie或session的登录模块

cookie文件夹下:

//FILE: login.php
<?php
header('Content-type:text/html;charset=utf-8');
if (isset($_COOKIE['username']) && $_COOKIE['username']==='robin') {
	exit('您已经登录请不要重复登录');
}
if(isset($_POST['submit'])){
	if(isset($_POST['username']) && isset($_POST['password']) && $_POST['username']==='robin' && $_POST['password']==='123456'){
		if(setcookie('username',$_POST['username'],time()+3600)){
			header('Location:skip.php?url=index.php&info=登录成功,正在跳转中!');
		}else{
			echo 'cookie设置失败!';
		}
	}else{
		header('Location:skip.php?url=index.php&info=对不起,用户名或密码填写错误,登录失败!');
	}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title>请登录</title>
</head>
<body>
<form method="post" action="login.php">
	姓名:<input type="text" name="username" />
	密码:<input type="password" name="password" />
	<input type="submit" name="submit" value="登录" />
</form>
</body>
</html>
//FILE: logout.php
<?php
header('Content-type:text/html;charset=utf-8');
if (isset($_COOKIE['username']) && $_COOKIE['username']==='robin') {
	if(setcookie('username',$_POST['username'],time()-3600)){
		header('Location:skip.php?url=index.php&info=注销成功,正在跳转中!');
	}else{
		header('Location:skip.php?url=index.php&info=注销失败,请稍后重试!');
	}
}
?>
//FILE: index.php
<?php
header('Content-type:text/html;charset=utf-8');
if (isset($_COOKIE['username']) && $_COOKIE['username']==='robin') {
	echo "尊敬的{$_COOKIE['username']}您好,欢迎回来!";
	echo "<a href='logout.php'>注销</a>";
}else {
	echo "<a href='login.php'>请登录</a>";
}

?>
//FILE: skip.php
<?php 
if (!isset($_GET['url']) || !isset($_GET['info'])) {
	exit();
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta http-equiv="refresh" content="3;URL=<?php echo $_GET['url'] ?>" />
<title>正在跳转中...</title>
</head>
<body>
	<div style="text-align:center;font-size:20px;"><?php echo $_GET['info'] ?>,3秒后自动跳转!</div>
</body>
</html>

session文件夹下:

//FILE: index.php
<?php
session_start();
header('Content-type:text/html;charset=utf-8');
if (isset($_SESSION['username']) && $_SESSION['username']==='robin') {
	echo "尊敬的{$_SESSION['username']}您好,欢迎回来!";
	echo "<a href='logout.php'>注销</a>";
}else {
	echo "<a href='login.php'>请登录</a>";
}
 
?>
//FILE: skip.php
<?php
if (!isset($_GET['url']) || !isset($_GET['info'])) {
	exit();
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta http-equiv="refresh" content="3;URL=<?php echo $_GET['url'] ?>" />
<title>正在跳转中...</title>
</head>
<body>
	<div style="text-align:center;font-size:20px;"><?php echo $_GET['info'] ?>,3秒后自动跳转!</div>
</body>
</html>
//FILE: login.php
<?php
session_start();
header('Content-type:text/html;charset=utf-8');
if (isset($_SESSION['username']) && $_SESSION['username']==='robin') {
	exit('您已经登录请不要重复登录');
}
if(isset($_POST['submit'])){
	if(isset($_POST['username']) && isset($_POST['password']) && $_POST['username']==='robin' && $_POST['password']==='123456'){
		$_SESSION['username']=$_POST['username'];
		header('Location:skip.php?url=index.php&info=登录成功,正在跳转中!');
	}else{
		header('Location:skip.php?url=index.php&info=对不起,用户名或密码填写错误,登录失败!');
	}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title>请登录</title>
</head>
<body>
<form method="post" action="login.php">
	姓名:<input type="text" name="username" />
	密码:<input type="password" name="password" />
	<input type="submit" name="submit" value="登录" />
</form>
</body>
</html>
//FILE: logout.php
<?php
session_start();
header('Content-type:text/html;charset=utf-8');
if (isset($_SESSION['username']) && $_SESSION['username']==='robin') {
	session_unset();//Free all session variables
	session_destroy();//销毁一个会话中的全部数据
	setcookie(session_name(),'',time()-3600,'/');//销毁保存在客户端的卡号(session id)
	header('Location:skip.php?url=index.php&info=注销成功,正在跳转中!');
}else{
	header('Location:skip.php?url=index.php&info=注销失败,请稍后重试!');
	
}
?>
发布了198 篇原创文章 · 获赞 82 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/weixin_38134491/article/details/104177009