In PHP, arrays can be stored in cookies for easy retrieval on subsequent requests. However, it is crucial to consider the security implications and use proper serialization techniques.
To convert the array into a cookie-compatible format, you can choose from the following methods:
setcookie('your_cookie_name', json_encode($info), time() 3600);
This method is effective for arrays consisting solely of integers:
$encodedArray = implode(',', $info);
setcookie('your_cookie_name', $encodedArray, time() 3600);
Caution: Avoid using serialize/unserialize due to potential security risks.
An alternative option is to store array elements individually in separate cookies:
setcookie('my_array[0]', 'value1' , time() 3600);
setcookie('my_array[1]', 'value2' , time() 3600);
setcookie('my_array[2]', 'value3' , time() 3600);
When you print the $_COOKIE variable, it will contain the array as follows:
echo '<pre>';
print_r( $_COOKIE );
die();
Array
(
[my_array] => Array
(
[0] => value1
[1] => value2
[2] => value3
)
)
This non-serialization approach is a documented PHP feature that stores cookie names as array names, allowing retrieval as arrays in PHP scripts.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3