1

I am doing some modifications for a site built primarily in ASP. However, the mods will be in PHP as the site is being moved over to that language.

When the user signs in, they are assigned cookies that look lkie so:

("mycook")("id")=23
("mycook")("pref")="HTML"
("mycook")("job")="janitor"

Now in asp, these can be referenced as:

request.cookies("mycook")("pref")

which would respond as "HTML"

Is there a similar syntax is PHP that anyone is aware of?

This doesn't seem to work:

echo $_COOKIE['mycook']['pref'];
echo $_COOKIE["mycook"]["pref"];

I saw a solution that uses a For Each -> , and I can see how that would work. But it just seems a bit of overkill (to loop through all the values just to print the one I am looking for) and I was wondering if anyone had any ideas?

Thanks in advance for your help.

1
  • 1
    I you use var_dump($_COOKIE); you'll be able to see what's in that variable -- and see the structure of your cookies. Try using that, and edit your question with that structure. Commented Jul 20, 2011 at 19:57

2 Answers 2

1

In your example, your cookies will be stored as the following string in the "mycook" cookie:

["mycook"]=>string(27) "pref=HTML&job=janitor&id=23"

So to access you will need echo $_COOKIE['mycook'] then translate the url encoded string into something more useful.

parse_str($_COOKIE['mycook'], $mycook);
echo $mycook['pref'];

If you don't need to have second level cookies, just assigning as:

Response.Cookies("id")=23
Response.Cookies("pref")="HTML"
Response.Cookies("job")="janitor"

Will allow you to access the cookies in PHP with just:

echo $_COOKIE['pref'];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply. I would love if they didn't use second level cookies, but they do, and they are throughout the site. Thanks for your help with parse_str, that was what I was looking for.
0

here is a few link that will help:

http://www.tizag.com/phpT/phpcookies.php

http://php.net/manual/en/function.setcookie.php

Comments

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.