Home > php教程 > php手册 > body text

php 给cookie赋值之setcookie()用法

WBOY
Release: 2016-06-13 10:16:51
Original
967 people have browsed it

今天早想做一个功能用户登录之后我把用户的名字保存到cookie中但是我开始使用读取cookie 的方法,但是没效果,后来发现使用setcookie()后面必须带时间才可以,下面我来介绍一下。

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

参数     描述
name 必需。规定 cookie 的名称。
value 必需。规定 cookie 的值。
expire 可选。规定 cookie 的有效期。
path 可选。规定 cookie 的服务器路径。
domain 可选。规定 cookie 的域名。
secure 可选。规定是否通过安全的 HTTPS 连接来传输 cookie。


例,一个简单给cookie赋值

 代码如下 复制代码

$value = "my cookie value";

// 发送一个 24 小时候过期的 cookie
setcookie("TestCookie",$value, time()+3600*24);
?>


例,二维数组存储到cookie

a.php

 代码如下 复制代码


$cart_info[0][0] = "1123";
$cart_info[1][0] = "5334521";
$cart_info[1][2] = "df";
$cart_info[4][2] = "fefe";

setcookie("xia",serialize($cart_info));
?>

b.php

 代码如下 复制代码


$other = StripSlashes($_COOKIE['xia']);
print_r(unserialize($other));
?>

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!