Cookie隐藏小广告

方式一:在close.php页面上设置COOKIE,

colse.php页面

<?php

setcookie('hide','1');
header('Location: indes.php');

index.php页面 

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	div{
		height:200px;
		width:200px;
		background-color: pink;
	}
    </style>
</head>
<body>
	<?php if(empty($_COOKIE['hide'])||$_COOKIE['hide']!='1'): ?> //如果COOKIE没有设置,还是会显示
	<div calss='ad'>
		<a  href="close.php">不再显示</a>
	</div>
    <?php endif ?>
	
</body>
</html>

 

方式二:因为点击index.php回车键访问该页面,和点击a连接都是访问的同一个页面,所以当想要关闭的时候我们要让浏览器能够区分,所有通过传递参数的方式,当收到这个参数的时候就设置一个COOKIE

 

<?php

if(isset($_GET['action'])&&$_GET['action']=='hide'){
	setcookie('hide2','2');
    $_COOKIE['hide2'];
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	div{
		height:200px;
		width: 200px;
		background-color: pink;
	}
    </style>
</head>
<body>
	<?php if(empty($_COOKIE['hide2'])||$_COOKIE['hide2']!=='2'):?>
	<div>
		<a href="index2.php?action=hide">不再显示</a>
	</div>
    <?php endif ?>
</body>
</html>

 

posted @ 2018-11-06 15:56  mysunshine_SZZ  阅读(176)  评论(0编辑  收藏  举报