这篇文章主要介绍了php中cookie的使用方法,包括创建/更新cookie、删除cookie、取cookie值等用法,需要的朋友可以参考下
1.创建/更新cookie
复制代码 代码如下:
setCookie($cookieName,$value,time()+秒数);
例子:创建一个cookie,名字为UserName,值为zs,过期时间为2个星期
复制代码 代码如下:
setcookie("UserName","zs",time()+2*7*24*3600);
如果不设置时间,,就不会保存到cookie文件中。浏览器不关时,能够访问。当浏览器关闭时,就无法访问了。
例子:
复制代码 代码如下:
setcookie("Age","18");
2.取cookie的值
复制代码 代码如下:
$_cookie[$cookieName];
例子:取出UserName的值,放在变量$uName中
复制代码 代码如下:
$uName=$_COOKIE['UserName'];
取值的时候,一般会先判断是否为空,再进行取值操作。上面的取值操作一般这样写:
复制代码 代码如下:
if (!empty($_COOKIE['UserName']))
{
$uName=$_COOKIE['UserName'];
}
3.删除指定cookie
复制代码 代码如下:
setcookie($cookieName,value,time()-秒数);
//或者
setcookie($cookiename, '');
//或者
setcookie($cookiename, NULL);
例子:删除UserName
复制代码 代码如下:
setcookie("UserName","zs",time()-3600);
4.删除当前会话的所有cookie
复制代码 代码如下:
foreach($_COOKIE as $key=>$val){
setcookie($key,"",time()-100);
}
当无cookie时,本机保存cookie的文件也将删除。

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

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.
