Home >Backend Development >PHP Problem >How to clear history in php

How to clear history in php

藏色散人
藏色散人Original
2021-12-14 09:23:021979browse

How to clear the history in php: 1. Create a PHP sample file; 2. Set the cookie through setcookie; 3. Clear the history through "echo 'clear';" and other diam.

How to clear history in php

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

php How to clear history?

php simply implements cookie history and clearing history functions

The code is as follows:

<!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(&#39;1:标题1&#39;,&#39;内容1……&#39;),
2 => array(&#39;2:标题2&#39;,&#39;内容2……&#39;),
3 => array(&#39;3:标题3&#39;,&#39;内容3……&#39;),
4 => array(&#39;4:标题4&#39;,&#39;内容4……&#39;),
);
 
$id=isset($_GET[&#39;id&#39;])?$_GET[&#39;id&#39;]:1;
 
//判断有无设置action
$action = isset($_GET[&#39;action&#39;])?$_GET[&#39;action&#39;]:$id; 
if($action==&#39;clear&#39;){
//清空cookie
setcookie(&#39;history&#39;,&#39;&#39;,time()-3600);
//数组清空
$his=array();
}else{
if(!isset($_COOKIE[&#39;history&#39;])){
$his[]=$id;
}else{
$his=explode(&#39;|&#39;,$_COOKIE[&#39;history&#39;]);
array_unshift($his, $id);
$his=array_unique($his);
if(count($his)>4){
array_pop($his);
}
}
}
setcookie(&#39;history&#39;,implode(&#39;|&#39;, $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 &#39;clear&#39;;?>">清除记录</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>

Copy and paste to run~

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to clear history in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn