1

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

3
  • 5
    Why do you need to store 600 dates in a cookie? And what kind of data is that? Commented Dec 14, 2009 at 19:06
  • 1
    How about making a JSON array and in php use json_decode Commented Dec 14, 2009 at 19:10
  • @Alex L: edit your answer to add that! Commented Dec 14, 2009 at 19:15

1 Answer 1

1

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 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.

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?

Sign up to request clarification or add additional context in comments.

2 Comments

I have to interject and recommend the phpjs.org serialize function here.
@fucla - thanks, that looks good. I've never actually needed to bother serializing JS data, so I'll have to remember that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.