search
Homephp教程php手册php cookie记录用户访问过的网页方法

cookie 常用于识别用户。cookie 是服务器留在用户计算机中的小文件。每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie。通过 php教程,您能够创建并取回 cookie 的值。
如何创建 cookie?
setcookie() 函数用于设置 cookie。

注释:setcookie() 函数必须位于 标签之前。

语法
setcookie(name, value, expire, path, domain);

$content_id = array();//1.创建一个数组 
$content_id[] = $_get['contentid']; //2.对接受到的id插入到数组中去
if(isset($_cookie['content_id'])) //3.判定cookie是否存在,第一次不存在(如果存在的话) 
{ 
$now_content = str_replace("", "", $_cookie['content_id']);//(4).您可以查看下cookie,此时如果unserialize的话出问题的,我把里面的斜杠去掉了 
$now = unserialize($now_content); //(5).把cookie 中的serialize生成的字符串反实例化成数组 
foreach($now as $n=>$w) { //(6).里面很多元素,所以我要foreach 出值 
if(!in_array($w,$content_id)) //(7).判定这个值是否存在,如果存在的化我就不插入到数组里面去; 
{ 
$content_id[] = $w; //(8).插入到数组 
} 
} 
$content= serialize($content_id); //(9).把数组实例化成字符串 
setcookie("content_id",$content, time()+3600*24); //(10).插入到cookie
}else { 
$content= serialize($content_id);//4.把数组实例化成字符串 
setcookie("content_id",$content, time()+3600*24); //5.生成cookie 
}
$getcontent = unserialize(str_replace("", "", $_cookie['content_id'])); 
/*foreach($getcontent as $row=>$r) 
{ 
echo $r;//(取值) 
}*/
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.