Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to access Array by cookie in php

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the php cookie how to access the array, the article is very detailed, has a certain reference value, interested friends must read it!

Cookie cannot store arrays by default, so the following is incorrect.

The error is as follows:

Warning: setcookie () expects parameter 2 to be string, array given in

But PHP can parse cookie with the same name followed by [] into an array. The method to implement cookie memory array in php is as follows:

Method 1: first serialize the array with serialize, then store it in COOKIE, and read it out with unserialize to get the original array.

Method 2: set the multi-key value cookie, pay attention to the key value must be given

The copy code is as follows:

$arr = array (1pm 2pm 3)

Setcookie ("a [0]", $arr [0])

Setcookie ("a [1]", $arr [1])

Setcookie ("a [2]", $arr [2])

The result: all the elements of the array are stored in.

Array length: 3

Array ([0] = > 1 [1] = > 2 [2] = > 3)

The following words are incorrect:

$arr = array (1pm 2pm 3)

Setcookie ("a []", $arr [0])

Setcookie ("a []", $arr [1])

Setcookie ("a []", $arr [2])

Result: only the last element is saved

Array length: 1

Array ([0] = > 3)

These are all the contents of the article "how to access arrays in cookie in php". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report