Home  >  Article  >  Backend Development  >  How to set cookie array in php

How to set cookie array in php

藏色散人
藏色散人Original
2021-07-12 09:39:222152browse

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]]]]]);

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

Get the cookie

echo $_COOKIE['name'];

Delete cookies

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

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;
}

Method two:

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

$serArr = serialize($arr);

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

print_r( unserialize($_COOKIE['intro']) );

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!

Statement:
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