views:

47

answers:

2

Hello!

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.

+3  A: 

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 G. Vicario
+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
Yes, this is how the browser gets the cookies. And it can read it?
Christopher
@Christopher, can you be more precise? What don't you understand exactly?
Álvaro G. Vicario
The problem is that the receiving end doesn't read the cookies.
Christopher
What receiving end are you talking about? Have you printed the value of `$_SERVER['HTTP_COOKIE']` on screen?
Álvaro G. Vicario
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
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 G. Vicario
A: 

Thank you for your answers.

I'm trying to add this using cURL.

if(isset($cookie)) {
curl_setopt($ch, CURLOPT_COOKIE, $cookie);

}

The problem is not the code using cURL. Because I've tried to add other cookies and it works perfectly.

But when I'm using brackets it doens't....

Maybe I could set my own header with php cURL?

Christopher
@Christopher, if you need to provide further info you should append it to your original question. Please do not insert it as an answer since it's not an answer.
Álvaro G. Vicario
Okej, sorry about that.
Christopher