views:

20

answers:

1

ASP.Net has the concept of using 'subkeys' in cookies. i.e. You can write a cookie with

Response.Cookies("userInfo")("userName") = "patrick"
Response.Cookies("userInfo")("lastVisit") = "today"

This would create a cookie which looks like

Name:   userInfo
Value:  userName=patrick:lastVisit=today

Is there a native method in PHP to read/write cookies like the above one?

I need to read/write a cookie in PHP which can be read by ASP.Net with subkeys

+1  A: 

To write such a cookie:

$userInfo = array(
     'userName'  => 'patrick'
    ,'lastLogin' => 'today');

$userInfo = str_replace('&', ':', http_build_query($userInfo));
setcookie('userInfo', $userInfo);

to parse the cookie back into an array:

$userInfo = parse_str(str_replace(':', '&', $_COOKIE['userInfo'));
mmattax
I'm particularly facing problems while writing a cookie in this format:"subkey=value:subkey2=value". PHP is having problems writing unescaped data to the cookie too. It converts '=' to %3D
Swizzy
@Swizzy Use urldecode to decode the string.
mmattax
Yes I could user urldecode, but I'll have to rewrite all ASP.Net code to do a similar manipulation. Is there any way the cookie can contain "userName=patrick:lastVisit=today" instead of " userName%3Dpatrick%3AlastLogin%3Dtoday" ?
Swizzy