Cookie With Multi Dimensional Array Cannot Be Read (Sometimes) Is This A Bug?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Cookie With Multi Dimensional Array Cannot Be Read (Sometimes) Is This A Bug?

Baz L
*Sigh*,

So I've been pulling out my hair for a while until I figured, this thing just doesn't work the way it should.

Here's my scenario very simplified with test data:

Controller1:

$arr['name']['1'] = '21';
$arr['name']['2'] = '21';
$arr['name1']['1'] = '21';
$this->Cookie->write('zilla', $arr, false);
$this->log('What\'s my cooking');
$this->log($this->Cookie->read('zilla'));

Output:

(
    [name] => Array
        (
            [1] => 21
            [2] => 21
        )

    [name1] => Array
        (
            [1] => 21
        )

)

Controller 2:

$this->log('What\'s my cooking');
$this->log($this->Cookie->read('zilla'));

Output:
(
    [name] => Array
    [name1] => Array
)

It basically eats up the second dimension on an array.
Any ideas?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Cookie With Multi Dimensional Array Cannot Be Read (Sometimes) Is This A Bug?

Ivolution

*Probably* the following will work:

before writing your array to the cookie:
$arr = serialize($arr);

after reading it from your cookie (when $cookval is the value you read
from your cookie):
$arr = @unserialize($cookval);

This way you should have your whole array back.

Note however that I didn't test this code at all, it's just the first
thing that jumps into my mind.

Ivo


On Nov 11, 12:45 pm, Baz <[hidden email]> wrote:

> *Sigh*,
>
> So I've been pulling out my hair for a while until I figured, this thing
> just doesn't work the way it should.
>
> Here's my scenario very simplified with test data:
>
> Controller1:
>
> $arr['name']['1'] = '21';
> $arr['name']['2'] = '21';
> $arr['name1']['1'] = '21';
> $this->Cookie->write('zilla', $arr, false);
> $this->log('What\'s my cooking');
> $this->log($this->Cookie->read('zilla'));
>
> Output:
>
> (
>     [name] => Array
>         (
>             [1] => 21
>             [2] => 21
>         )
>
>     [name1] => Array
>         (
>             [1] => 21
>         )
>
> )
>
> Controller 2:
>
> $this->log('What\'s my cooking');
> $this->log($this->Cookie->read('zilla'));
>
> Output:
> (
>     [name] => Array
>     [name1] => Array
> )
>
> It basically eats up the second dimension on an array.
> Any ideas?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Cookie With Multi Dimensional Array Cannot Be Read (Sometimes) Is This A Bug?

Baz L
Thanks for the response, but here's a few FYIs.

  • What I posted before works fine with Sessions. That's actually how I determined the problem. I was building a "remember me" for a login. And I thought I could just dump the session variable in a cookie and retrieve later. So maybe if anyone decides to fix this, they might look at the Session::write and Session::read.
  • I don't know much about cookies, but it appears that after the cookie is saved AND the pages has loaded, we have problems.
  • Now here's the kicker. Your solutions seems fine. And it might be, but I've noticed that it doesn't work when the cookie is NOT encrypted. I was storing non-encrypted while debug so I could look at values, but when I switched to encrypted, it worked like a charm. Go figure.
  • Another FYI, it failed when I tried to store: $this. The initial write and read worked, but from the second controller, it failed to read. I'm guessing max cookie or something?
Maybe I'm trying to do too much. But regular, small, single dimensional arrays work fine. And the serialize options works for small multi dimensional arrays.

ThanX again.

On Nov 11, 2007 9:20 AM, Ivolution <[hidden email]> wrote:

*Probably* the following will work:

before writing your array to the cookie:
$arr = serialize($arr);

after reading it from your cookie (when $cookval is the value you read
from your cookie):
$arr = @unserialize($cookval);

This way you should have your whole array back.

Note however that I didn't test this code at all, it's just the first
thing that jumps into my mind.

Ivo


On Nov 11, 12:45 pm, Baz <[hidden email]> wrote:

> *Sigh*,
>
> So I've been pulling out my hair for a while until I figured, this thing
> just doesn't work the way it should.
>
> Here's my scenario very simplified with test data:
>
> Controller1:
>
> $arr['name']['1'] = '21';
> $arr['name']['2'] = '21';
> $arr['name1']['1'] = '21';
> $this->Cookie->write('zilla', $arr, false);
> $this->log('What\'s my cooking');
> $this->log($this->Cookie->read('zilla'));
>
> Output:
>
> (
>     [name] => Array
>         (
>             [1] => 21
>             [2] => 21
>         )
>
>     [name1] => Array
>         (
>             [1] => 21
>         )
>
> )
>
> Controller 2:
>
> $this->log('What\'s my cooking');
> $this->log($this->Cookie->read('zilla'));
>
> Output:
> (
>     [name] => Array
>     [name1] => Array
> )
>
> It basically eats up the second dimension on an array.
> Any ideas?



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Cookie With Multi Dimensional Array Cannot Be Read (Sometimes) Is This A Bug?

Grant Cox

The reason it works in the first controller is because it is not
reading from the cookie itself - it also saves the data to a local
variable and it is reading back from that.  Because there is no
automatic serialization going on, only strings are written to the
cookie - so a complex data type will never work in your second
controller (or after refreshing the page, or whatever so that it does
actually read the cookie).

I can't think of any reason that it would not work without encryption
though...


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Cookie With Multi Dimensional Array Cannot Be Read (Sometimes) Is This A Bug?

Ivolution

I agree with Grant Cox..
You can't just write any variable to a cookie. With plaintext strings
this works, but when you have complex datatypes like multidimensional
arrays, you have to convert it somehow to a plaintext string.
This is exactly where the serialize() function kicks in: the only
thing this function does is generate a storable representation
(plaintext string) of any value.
So there's no encryption involved at all (though it looks encrypted if
you look at the value). If you want to encrypt your information (seems
to be a good thing since it's about user-logins), you should serialize
your array, and then encrypt it's output before you pass it to the
cookie. When retrieving the cookie, it's obviously the other way
round.


On Nov 12, 5:43 am, Grant Cox <[hidden email]> wrote:

> The reason it works in the first controller is because it is not
> reading from the cookie itself - it also saves the data to a local
> variable and it is reading back from that.  Because there is no
> automatic serialization going on, only strings are written to the
> cookie - so a complex data type will never work in your second
> controller (or after refreshing the page, or whatever so that it does
> actually read the cookie).
>
> I can't think of any reason that it would not work without encryption
> though...


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Cookie With Multi Dimensional Array Cannot Be Read (Sometimes) Is This A Bug?

Baz L
Sorry, didn't know cookies were plain text.

So serialization should work always, but it doesn't if you set the encryption option to off.
I'm just sharing information trying to save others the hours I've spent with this problem.
--
Baz L
Web Development 2.0: Web Design, CakePHP, Javascript
http://www.WebDevelopment2.com/

On Nov 12, 2007 11:31 AM, Ivolution < [hidden email]> wrote:

I agree with Grant Cox..
You can't just write any variable to a cookie. With plaintext strings
this works, but when you have complex datatypes like multidimensional
arrays, you have to convert it somehow to a plaintext string.
This is exactly where the serialize() function kicks in: the only
thing this function does is generate a storable representation
(plaintext string) of any value.
So there's no encryption involved at all (though it looks encrypted if
you look at the value). If you want to encrypt your information (seems
to be a good thing since it's about user-logins), you should serialize
your array, and then encrypt it's output before you pass it to the
cookie. When retrieving the cookie, it's obviously the other way
round.


On Nov 12, 5:43 am, Grant Cox <[hidden email]> wrote:

> The reason it works in the first controller is because it is not
> reading from the cookie itself - it also saves the data to a local
> variable and it is reading back from that.  Because there is no
> automatic serialization going on, only strings are written to the
> cookie - so a complex data type will never work in your second
> controller (or after refreshing the page, or whatever so that it does
> actually read the cookie).
>
> I can't think of any reason that it would not work without encryption
> though...




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Loading...