2

I'm trying to use brackets within the name of a cookie.

It is supposed to look like this(this is how the browser wants it!):

Name: name[123456].newName
Content: 20

Here is my example:

$cookie = "name[123456].newName=20"

But when I analyze what the browser sees, I get this:

cookie['name'] = Array

And I want:

cookie['name[123456].newName'] = 20

My question is: How should I write the cookies name in a way that the browser understands?

Thank you in advance.

Simon East
  • 55,742
  • 17
  • 139
  • 133
  • 2
    Arggh. This is bound to cause problems. Why do you need this? – Pekka Oct 19 '10 at 14:32
  • That sure is horrible, but they are all allowed chars for cookies, can you give a more complete example of how you add the cookie for curl? – Hannes Oct 19 '10 at 14:44
  • When I check the cookie in chrome it looks like this: name[123456].newName And when I check the cookie in a webformanalyzer the cookie is like I showed in the previous example. – Christopher Oct 20 '10 at 13:14

1 Answers1

5

Actually, all you have to do is this:

<?php

setcookie('name[123456].newName', 20);

?>

This generates the following header:

Set-Cookie: name[123456].newName=20

... and browsers (well, at least Firefox) seem to handle it just fine.

The issue starts when you want to read the value back. PHP has an otherwise nice feature: whenever it finds an input parameter (get, post, cookie...) with square brackets on its name, it'll build an array from it. So print_r($_COOKIE) displays this:

Array
(
    [name] => Array
        (
            [123456] => 20
        )

)

I'm not aware of any way to disable this feature so you probably need to use string functions and parse the contents of the raw cookie, which can be found at $_SERVER['HTTP_COOKIE']:

name[123456].newName=20
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 2
    +1. I don't think this feature is nice though; actually I think it's a serious PHP design flaw. You can often trip scripts up by sending them an array `a[]` where they are expecting a single value `a` (leaving them with the wrong datatype!), and they can't parse multiple inputs with the same name without `[]`. It would have been much better to have separate superglobals for single-value inputs and multiple-value inputs. – bobince Oct 19 '10 at 15:08
  • Yes, this is how the browser gets the cookies. And it can read it? – Christopher Oct 20 '10 at 08:12
  • @Christopher, can you be more precise? What don't you understand exactly? – Álvaro González Oct 20 '10 at 09:45
  • The problem is that the receiving end doesn't read the cookies. – Christopher Oct 20 '10 at 13:08
  • What receiving end are you talking about? Have you printed the value of `$_SERVER['HTTP_COOKIE']` on screen? – Álvaro González Oct 20 '10 at 14:58
  • Thank you for your answers, when I print out $_SERVER['HTTP_COOKE'] I get the correct cookies. The problem is still that the webpage(the receiving end) is not understanding the values I'm sending. It's making me mad! Example: `"name[834788].newName=20; name[865063].newName=20;"` – Christopher Oct 24 '10 at 13:42
  • Well, the value at `$_SERVER['HTTP_COOKE']` contains what the other site sent back. And I'm afraid that "does not understand" is not a better description that "does not work". Are you coding the remote site as well? – Álvaro González Oct 24 '10 at 16:10