Home  >  Article  >  Backend Development  >  How to set an array in php cookie and assign a value to the array

How to set an array in php cookie and assign a value to the array

PHPz
PHPzOriginal
2023-04-24 14:46:00624browse

As a scripting language widely used in Web development, PHP's cookie function occupies a very important place. A cookie is a web server that saves small text information on the client through the client's browser and sends it automatically when the client visits the website, so that the server can obtain the information and perform related operations. The most commonly used examples of cookies are to keep the user logged in after logging in, or to record the user's browsing history information, etc.

In PHP, setting cookies is very simple, just use the setcookie function. For example:

setcookie("username", "Jack", time()+3600); //设置用户名为"Jack",有效期为1小时

This code will create a cookie named "username" in the client browser and set its value to "Jack" with a validity period of 3600 seconds (1 hour). The value of the cookie can be read and modified in subsequent programs through the $_COOKIE global variable. For example:

echo $_COOKIE["username"]; //输出"Jack"

But, if we want to store an array in a cookie, how should we do it?

PHP provides a simple and effective method: serialize the array and store it in a cookie, and then deserialize it into an array when needed. For example:

$myArray = array("apple", "banana", "orange");
setcookie("fruits", serialize($myArray), time()+3600); //存储数组$myArray,并设置有效期为1小时

This code will create a cookie named "fruits" in the cookie and set its value to the serialized $myarray array with a validity period of 3600 seconds (1 hour).

We can use the unserialize function to deserialize the serialized array in the cookie to get:

$myCookieArray = unserialize($_COOKIE["fruits"]); //反序列化$_COOKIE["fruits"]的值为一个数组$myCookieArray
print_r($myCookieArray); //输出$myCookieArray数组

In this way, we can store and obtain an array in the cookie.

But it should be noted that the size of cookies is limited, and the size limits of different browsers and different servers are also different. If the stored array is too large, the cookie may not be saved properly or part of the data may be lost. Therefore, it is generally recommended to store simple data structures in cookies, such as strings, numbers, Boolean values, etc. It is not recommended to store data structures that are too large or too complex.

In general, the use of cookies in PHP is very flexible and convenient, and it is also a commonly used technology in Web development. For some simple data structures, we can store them in cookies through serialization and deserialization to facilitate subsequent operations. Of course, you also need to pay attention to issues such as cookie size restrictions and security.

The above is the detailed content of How to set an array in php cookie and assign a value to the array. 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