How to set cookie array in php

藏色散人
Release: 2023-03-11 12:40:01
Original
2161 people have browsed it

In PHP, you can set the cookie array through the "setcookie()" function. The syntax format is "setcookie('name', 'tom', time() 60, '/', '', false);".

How to set cookie array in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

php Set cookie array

The syntax format of the setcookie() function is as follows:

bool setcookie(string name[,string value[,int expire[,string path[,string domain[,int secure]]]]]);
Copy after login

The parameter description is as follows:
name Cookie name
value Cookie value
expire Cookie expiration time
path The valid path of the cookie on the server side
domain The valid domain name of the cookie
source Indicates whether the cookie is passed through secure HTTPS

Set the cookie

setcookie('name', 'tom', time()+60, '/', '', false);
Copy after login

Get the cookie

echo $_COOKIE['name'];
Copy after login

Delete cookies

setcookie('name', 'tom', time()-60, '/', '', false);
Copy after login

Set cookie array
Method one:

setcookie('profile[name]', 'zhangsan');        
setcookie('profile[gender]', 'male');        
setcookie('profile[age]', 24);        
foreach($_COOKIE['profile'] as $k=>$v) {
    echo $k.':'.$v;
}
Copy after login

Method two:

$arr = array(
        'name'=>'tom',
        'gender'=>'male',
        'age'=>28
    );

$serArr = serialize($arr);

setcookie('intro', $serArr, time()+60);

print_r( unserialize($_COOKIE['intro']) );
Copy after login

How to set cookie array in php

Recommended learning: 《PHP Video Tutorial


The above is the detailed content of How to set cookie array in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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