php中获取cookie值非常的简单只要COOKIE[]中间是cookie id名就可以获取到了,下面来简单的给大家介绍下php中cookie的一个使用例子。
给cookie赋值
setcookie (name, value, expire, path, domain)
例如:
代码如下 | 复制代码 |
setcookie(“user”, “Alex Porter”, time()+3600); |
如果我们要获取user值如何操作
代码如下 | 复制代码 |
echo $_COOKIE["user"]; |
如果我们没设置user cookie那么我们执行时会出错了,这样我们可以使用isset函数来加以判断。
代码如下 | 复制代码 |
if(isset($_COOKIE["user"])) |
中文总是乱码
比如“小伟”获取后是“%u5C0F%u4F1F”
这个其实不是乱码,而是unicode的编码,在php中有一个内置函数叫unicode_encode可以将一个unicode字符串转变为你想要的编码方式,函数原型为:string unicode_encode ( unicode input, string encoding )
这里有一个例子可以参考一下:
代码如下 | 复制代码 |
header ('Content-Type: text/plain; charset=ISO-8859-2'); |
例子 结合js+php实现页面浏览统计
代码如下 | 复制代码 |
// 浏览页面次数 |
当运行这个页面时服务器端会写入个cookie值,用于保存你访问该页面的次数。这里应用到了php的setcookie方法。
输出这个值:
现在来看如何使用js获取和设置cookie
代码如下 | 复制代码 |
var cookie = $.cookie(‘demoCookie’); var cookie = $.cookie(‘demoCookie’); $.cookie(‘demoCookie’,text,{expires: 7}); |
当点击保存链接的时候,将输入框的值写入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

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

Hot Article

Hot 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),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
