Home > Backend Development > PHP Tutorial > How to use setcookie() to assign value to cookie in php_PHP Tutorial

How to use setcookie() to assign value to cookie in php_PHP Tutorial

WBOY
Release: 2016-07-13 17:16:16
Original
1003 people have browsed it

This morning I wanted to make a function. After the user logged in, I saved the user's name in a cookie, but I started to use the method of reading the cookie, but it didn't work. Later I found that using setcookie() must be followed by time. Below I Let me introduce it.

Grammar
setcookie(name,value,expire,path,domain,secure)

Parameter Description
name required. Specifies the name of the cookie.
value required. Specifies the value of the cookie.
expire is optional. Specifies the validity period of the cookie.
path is optional. Specifies the server path for cookies.
domain is optional. Specifies the domain name for the cookie.
secure Optional. Specifies whether cookies are transmitted over a secure HTTPS connection.


For example, a simple assignment to a cookie

The code is as follows
 代码如下 复制代码

$value = "my cookie value";

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

Copy code


$value = "my cookie value";

代码如下 复制代码

$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));
?>

//Send a cookie that expires in 24 hours

setcookie("TestCookie",$value, time()+3600*24);

?>
 代码如下 复制代码

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

For example, two-dimensional array is stored in cookie

The code is as follows Copy code
$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
The code is as follows Copy code
$other = StripSlashes($_COOKIE['xia']);<🎜> print_r(unserialize($other));<🎜> ?> http://www.bkjia.com/PHPjc/628686.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628686.htmlTechArticleThis morning I wanted to make a function. After the user logged in, I saved the user's name to a cookie, but I started using read Cookie method, but it didn’t work. Later I found out that after using setcookie()...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template