当前位置:网站首页>PHP 读写 COOKIE
PHP 读写 COOKIE
2022-06-12 06:19:00 【Cwillchris】
1、 Cookie 介绍
在 PHP 中通过 setcookie()函数创建 Cookie。语法格式如下:
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string$domain [, bool $secure = false [, bool $httponly = false ]]]]]])
参数 | 说明 | 必填 | 举例 |
name | cookie 的名字 | 是 | 使用 $_COOKIE['cookiename'] 调用名为 cookiename 的 cookie。 |
value | cookie 的值,存放在客户端,不要存放敏感数据 | 是 | 假定 name 是‘cookiename’,可以通过 $_COOKIE[‘cookiename’] 取得其值。 |
expire | Cookie 过期的时间。这是个 Unix 时间戳,即从 Unix 纪元开始的秒数。换而言之, 通常用 time() 函数再加上秒数来设定 cookie 的失效期。或者用 mktime()来实 现。 | 否 | time( )+86400*30 将设定 cookie 30 天后失效。如果未设定,cookie 将会在会话结束后 (一般是浏览器关闭)失效。 |
path | Cookie 在服务器端的有效路径。 | 否 | 如果该参数设为 ‘/‘的话, cookie 就在整个 domain 内有 效,如果设为 ‘/foo/‘, cookie 就只在 domain 下的 /foo/目录及其子目录内有效,例如 /foo/bar/。默认值为设定 cookie 的当前目录。 |
domain | 该 cookie 有效的域名。 | 否 | 要使 cookie 能在 如 example.com 域名下的所有子域都有效的话,该参数应该设 为 ‘.example.com’。虽然 . 并不必须的,但加上它会兼容更多的浏览器。如果该参数设为 www.example.com 的话,就 只在 www子域内有效。 |
secure | 指明 cookie 是否仅通过安全的 HTTPS 连 接传送。当设成 TRUE 时,cookie 仅在安 全的连接中被设置。默认值为 FALSE。 | 否 | 0 或 1 |
httponly | 设为 true 后,只能通过 http 访问, javascript 无法访问;防止 xss 读取 cookie;php5.2 以上版本已支持 HttpOnly 参数的设置,同样也支持全局的 HttpOnly 的 设置,在 php.ini 中, session.cookie_httponly=ture 来开启全局 的 Cookie 的 HttpOnly 属性 | 否 | setcookie(“abc”, “test”,NULL, NULL, NULL, NULL, TRUE) |
2、创建cookie
创建名称为 name 的 cookie,值为 root
└─# vim cookie1.php
插入如下代码:
<?php
setcookie('name','root'); //设置名称为 name 的 cookie,值为 root
setcookie('cwillchris','will',time()+3600); //设置名称为 cwillchris 的 cookie 的值为will,有效时间为 3600 秒
echo date('Y-m-d H:i:s'); //输出当前时间
?>

浏览器访问http://192.168.98.30/cookie1.php,按F12,选择Storage,就可以看到cookie信息

注意:从图中可以看到 domain 没有设置默认为 当前站点域名,path 默认为 / ,也就是这个 cookie 在整个站点内是有效的,第一个 cookie 没有设置过期时间 expire,默认为 Session ,说明当 浏览器关掉后,会失效。第二个 cookie 过期时间为访问页面时开始 1 个小时候失效。
3、读取cookie
在 PHP 中可以直接通过超级全局数组$_COOKIE[]来读取浏览器端的 Cookie 值。
读取名为name的cookie
创建文件cookie2.php
─# vim cookie2.php
插入如下代码
<?php
echo $_COOKIE['name']; //读取名称为 name 的 cookie 的值并输出到当前页面
echo "<br/>"; //输出换行,为了显示更明显些,将两个 cookie 的值用 换行间隔
echo $_COOKIE['cwillchris']; //读取名称为 xuegod 的 cookie 的值并输出到当前页面
?>

浏览器访问http://192.168.98.30/cookie2.php, 显示出例 1 中创建的两个 cookie 的值

关闭浏览器,重新打开访问这个链接, 此时只显示名称为 cwillchris的 cookie 的值,因为第一个 cookie 没有设置过期时间,关闭浏览器 后会失效。而第二个 cookie 设置的过期时间为 1 个小时。

4、删除cookie
当 Cookie 被创建后,如果没有设置它的失效时间,其 Cookie 文件会在关闭浏览器时被自动删除。 如果要在关闭浏览器之前删除 Cookie 文件,方法有两种:一种是使用 setcookie()函数删除,另一种是 在浏览器中手动删除 Cookie。下面分别进行介绍
(1) 使用 setcookie()函数删除 Cookie
删除 Cookie 和创建 Cookie 的方式基本类似,删除 Cookie 也使用 setcookie()函数。删除 Cookie 只需要将 setcookie()函数中的第二个参数设置为空值,将第 3 个参数 Cookie 的过期时间设置 为小于系统的当前时间即可。
例如: 将 Cookie 的过期时间设置为当前时间减 1 秒,代码如下
setcookie("name", "", time()-1);
(2)在浏览器中手动删除 Cookie 在使用 Cookie 时,Cookie 自动生成一个文本文件存储在 IE 浏览器的 Cookies 临时文件夹中。在 浏览器中删除 Cookie 文件是非常便捷的方法
实战:删除还没有失效的cookie
浏览器访问http://192.168.98.30/cookie1.php,得到两个cookie

然后浏览器访问浏览器访问http://192.168.98.30/cookie2.php,可以看到两个cookie的值

(1) 使用 setcookie()函数删除 Cookie
创建文件 delcookie.php
└─# vim delcookie.php
插入如下代码
<?php
setcookie('cwillchris',null,time()-1); //删除名为cwillchris的值
?>

浏览器访问浏览器访问http://192.168.98.30/delcookie.php
然后再次刷新 192.168.98.30/cookie2.php

可见名称为 cwillchris,值为 xueshen 的 cookie 不显示了,已经被删除了
(2) 在浏览器中手动删除 Cookie
浏览器访问http://192.168.98.30/cookie1.php,重新得到两个cookie

按F12,点击Storage,右击选中要删除的cookie,选择Delete ,就可以了

浏览器访问http://192.168.98.30/cookie2.php, 值为 cwillchris 的 cookie 不显示了,已经被删除

边栏推荐
- About why GPU early-z reduces overdraw
- Redis队列
- Explanation of sensor flicker/banding phenomenon
- Nodemon cannot load the file c:\users\administrator\appdata\roaming\npm\nodemon PS1, because script execution is prohibited in this system
- sqlite交叉编译动态库
- Front desk display LED number (number type on calculator)
- Multithreading (2) -- pipeline (4) -- Park and unpark
- 交叉编译libev
- Logistic regression model
- Unity3d script captures a sub area from the screen and saves it as texture2d, which is used to save pictures and maps
猜你喜欢

(UE4 4.27) customize primitivecomponent

Redis queue

Leetcode January 10 daily question 306 Additive number

About why GPU early-z reduces overdraw

Simple spiral ladder generation for Houdini program modeling

MLP sensor

(UE4 4.27) customize globalshader

Three years of sharpening a sword: insight into the R & D efficiency of ant financial services

(UE4 4.27) add globalshder to the plug-in

Introduction to the method of diligently searching for the alliance procedure
随机推荐
Leetcode-646. Longest number pair chain
The first principle of thinking method
Unity3d multi platform method for reading text files in streamingasset directory
关于 Sensor flicker/banding现象的解释
Leetcode January 12 daily question 334 Increasing ternary subsequence
姿态估计之2D人体姿态估计 - PifPaf:Composite Fields for Human Pose Estimation
Chartextcnn (Ag dataset - news topic classification)
English grammar_ Adverb_ With or without ly, the meaning is different
Directx11 advanced tutorial tiled based deffered shading
Directx11 advanced tutorial cluster based deffered shading
Redis configuration (IV) -- cluster
Redis data type (VII) -- hyperloglog
Unity implements smooth interpolation
Why is the union index the leftmost matching principle?
Remap function of C different interval mapping
Computer composition and design work06 —— 基于MIPS
Explanation of sensor flicker/banding phenomenon
Multithreading Foundation (XI) -- prevent CPU from occupying 100%
MNIST handwritten data recognition by CNN
Overview of camera image quality