faqts : Computers : Programming : Languages : PHP : Common Problems : Cookies

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

6 of 7 people (86%) answered Yes
Recently 3 of 4 people (75%) answered Yes

Entry

Can I use an ASP style cookie dictionary in PHP?

Jul 7th, 1999 20:25
Nathan Wallace, Hendrik M.J. Arnoldus, Jason Brooke, Samuel Liddicott, Matthew Clark


PHP3 treats cookies as a simple "name"/"value" pair. In ASP, it is
possible to treat a cookie like a dictionary: i.e. one cookie can have a
set of "name"/"value" pairs for one cookie name; in that case, the
cookie is defined as a dictionary and has the property HasKeys as True. 
 
A dictionary cookie (URL Encoded) has a structure like:
 
MyCookie
ISMEMBER=True&REGISTEREDUSER=True&TCPADDR=111%2E222%2E33%2E44&DEPARTMENT=COMPSCIENCE&LASTNAME=DOE&FIRSTNAME=John&OS=WinNT
MyDomain/
0
1155155072
29280368
647743440
29280360
*

This translates to: (changed case for readability)
    ("MyCookie")("IsMember") = True
    ("MyCookie")("RegisteredUser") = True
    ("MyCookie")("TcpAddr") = 111.222.33.44
    ("MyCookie")("Department") = "COMPSCIENCE"
    ("MyCookie")("LastName") = "Doe"
    ("MyCookie")("FirstName") = "John"
    ("MyCookie")("OS") = "WinNT"
 
All other parameters as in a normal (non-dictionary) cookie.

All the ASP is doing here is reading out the string and arranging the
key/pairs for you in an array under the Cookie's name. In Php you get to
do it that way if you like, or also any of a number of other ways.

See the parse_str() function in the manual for starters - it'd be pretty
easy to make a quick small function to mimic what ASP is doing with the
cookie using this function.

Since the data that can be stored in a cookie is limited in size you may
like to consider creating a session for your users and storing the data
on the server.  See PHPLIB for an implementation:

    http://phplib.shonline.de