JavaScript和php中的cookie [英] cookie in javascript and php

查看:51
本文介绍了JavaScript和php中的cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can I set a multi-dimensional array 30x200 in JavaScript as a cookie and then read it back in PHP?

推荐答案

您可以根据需要。 Cookie标头的正常最大大小为4Kb,因此您最好确保数据大小不超过该大小。

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.

PHP和Javascript显然具有不同的语法方法处理数据,因此您必须以某种方式序列化数组并在PHP中执行相反的操作才能将数据转换为可用形式。如果需要保留特定的数组语法,则可以将数据编码为JSON字符串,然后在其中使用 json_decode() PHP。另一种方法是找到序列化类(请参阅本文)for Javascript。也许甚至用逗号分隔的字符串也可以工作,您可以在PHP中 explode()

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. If you need to keep specific array syntax, you could encode the data into a JSON string and use json_decode() in PHP. Another method would be to find a serialization class (see this article) for Javascript. Perhaps even a comma-delimited string would work that you could just explode() in PHP.

听起来好像有比cookie更好的方法。您可能需要重新检查您的方法。也许您可以将日期以某种方式存储在$ _SESSION中?

It sounds like there could be a better way to do this than cookies though. You may want to re-examine your methods. Perhaps you could store the dates in $_SESSION somehow?

这篇关于JavaScript和php中的cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆