简体   繁体   English

JavaScript和php中的cookie

[英]cookie in javascript and php

我可以在JavaScript中将30x200多维数组设置为cookie,然后在PHP中将其读回吗?

You can if you want. 您可以根据需要。 The normal maximum size for a cookie header is 4Kb, so you would do well to ensure that your data size does not exceed that. Cookie标头的正常最大大小为4Kb,因此您最好确保数据大小不超过该大小。

PHP and Javascript obviously have different syntactical methods of dealing with data, so you'd have to serialize your array somehow and perform the reverse in PHP to get the data into a usable form. PHP和Javascript显然具有处理数据的不同语法方法,因此您必须以某种方式序列化数组并在PHP中执行相反的操作才能将数据转换为可用形式。 If you need to keep specific array syntax, you could encode the data into a JSON string and use json_decode() in PHP. 如果需要保留特定的数组语法,则可以将数据编码为JSON字符串,然后在PHP中使用json_decode() Another method would be to find a serialization class ( see this article ) for Javascript. 另一种方法是找到Javascript的序列化类( 请参阅本文 )。 Perhaps even a comma-delimited string would work that you could just explode() in PHP. 甚至逗号分隔的字符串也可以使用PHP中的explode()

It sounds like there could be a better way to do this than cookies though. 听起来可能有比cookie更好的方法。 You may want to re-examine your methods. 您可能需要重新检查您的方法。 Perhaps you could store the dates in $_SESSION somehow? 也许您可以将日期以某种方式存储在$ _SESSION中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM