PHP实现将浏览历史页面网址保存到cookie的方法,phpcookie
本文实例讲述了PHP实现将浏览历史页面网址保存到cookie的方法。分享给大家供大家参考。具体如下:
将浏览历史页面网址保存到cookie,大致的思路如下面的代码,与实际应用有些差别。
复制代码 代码如下:
/*******
说明:cookie只能保存字符串 本实例中,需要保存多个URL(历史访问记录),思路是先将URL数组转为字符串,然后保存,读取时,再循环读取
*******/
//先假设当前URL为:http://localhost/php/?id=1
$id = $_GET['id'];
if(isset($_COOKIE['his'])){
$urls = $_COOKIE['his'];//读取cookie
$arr = unserialize($urls);//字符串转回原来的数组
$arr[] = $_SERVER['REQUEST_URI'];//当前页面url添加到数组中
$arr = array_unique($arr);//除去重复的
if(count($arr)>10){//只保存10条访问记录
array_shift($arr);
}
$urls = serialize($arr);//存储为字符串,
setcookie('his',$urls);//保存到cookie当中
}else{
$url = $_SEVER['REQUEST_URI'];//获取当前页面URL
$arr[] = $url;//将当前URL保存到数组中
$urls = serialize($arr);//存储为字符串
setcookie('his',$urls);//保存到cookie当中
}
echo "上一页
";//上一页,访问测试用
echo "下一页";//下一页,访问测试用
?>
历史访问页面
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools
