温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php如何清除历史

发布时间:2021-12-14 09:31:53 来源:亿速云 阅读:145 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关php如何清除历史,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

php清除历史的方法:1、创建一个PHP示例文件;2、通过setcookie设置cookie;3、通过“echo 'clear';”等diam清除历史记录即可。

php如何清除历史

本文操作环境:Windows7系统,PHP7.4版,Dell G3电脑。

php简单实现cookie的历史记录及清除历史记录功能

代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SQT</title>
<style>div{ width:300px;height:400px; background:#CFF; padding:20px}</style>
</head>
<?php
//用于测试的文档
$all_data = array(
1 => array('1:标题1','内容1……'),
2 => array('2:标题2','内容2……'),
3 => array('3:标题3','内容3……'),
4 => array('4:标题4','内容4……'),
);
 
$id=isset($_GET['id'])?$_GET['id']:1;
 
//判断有无设置action
$action = isset($_GET['action'])?$_GET['action']:$id; 
if($action=='clear'){
//清空cookie
setcookie('history','',time()-3600);
//数组清空
$his=array();
}else{
if(!isset($_COOKIE['history'])){
$his[]=$id;
}else{
$his=explode('|',$_COOKIE['history']);
array_unshift($his, $id);
$his=array_unique($his);
if(count($his)>4){
array_pop($his);
}
}
}
setcookie('history',implode('|', $his));
?>
 
<body>
<div>
<form >
<table>
    <tr><td><?php echo $all_data[$id][0]?></td></tr>
    <tr><td><?php echo $all_data[$id][1]?></td></tr>
    <tr>
    <td>  
        <p><a href="04.php?id=<?php echo $id-1==0?4:$id-1;?>">上一页</a></p>
        </td>
        <td>
         <p><a href="04.php?id=<?php echo $id+1==5?1:$id+1;?>">下一页</a></p>
        </td>
    </tr>
<tr>
     <td>
        <p>浏览历史</p>
     </td>
         <td>
        <a href="04.php?action=<? echo 'clear';?>">清除记录</a>
     </td>
    </tr>
        <tr>
         <ul>
         <td>
            <?php foreach($his as $v) {?>
            <li><a href="04.php?id=<? echo $v;?>"><?php echo $all_data[$v][0] ; ?></a></li>
            <?php } ?>
         </td>
</ul>
        </tr>
    </table>
</form>
</div>
</body>
</html>

复制粘贴即可运行~

关于“php如何清除历史”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI