|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
|
|
#1
|
|||
|
|||
|
cookies and arrays
Is it possible to set a cookie to an array as in
a=['1','2'] RESPONSE.setCookie('lecturer',a) as in the above example when I view my cookie all I see is "['1'" Can anyone help ? |
|
#2
|
||||
|
||||
|
The answer is yes, although it's not as easy as it could be. There are several ways you could do this so lets just step over a few.
1. store repr(list) and then reainmate this using the eval function. Although i only suggest doing this if you're are confident of the type of objects stored in the cookie because its possible to craft the cookie to execute arbitary code on the server! 2. Use the SerialCookie() or SmartCookie() class; but both of these have now been depreciated because of the security risk mentioned above and will eventually "go away" .3. Parse the cookie string and build a new list containing the new values. This is a lot safer simply because you can run tests thoughout your code to make sure the values are what you're looking for! Definatly safer IMO! Hope this helps, Mark. |
|
#3
|
|||
|
|||
|
Thanks
Thanks for the quick reply. I was considering giving up....
If I understand you correctly if I go for option 3 I need to name the elements in my cookie and store each one separately ie setCookie('lecturer', 'name0', a[0]) etc Is that right ? Alison Quote:
![]() |
|
#4
|
||||
|
||||
|
Ok thats option four
, i was working on the assumtion that you had a list already stored in a cookie like this.."[1, 2, 3, 4, 5, 6]" Where as i was talking about splitting this string into a usable list i.e. >>> string_list = "[1, 2, 3, 4, 5, 6]" >>> string_list = string_list[1:-1] >>> string_list '1, 2, 3, 4, 5, 6' >>> string_list.split(', ') ['1', '2', '3', '4', '5', '6'] >>> Which do you want to use or, what do want to do exactly? Any info you could give me would be very useful!Mark. |
|
#5
|
|||
|
|||
|
Thank you for your patience. As you can see I am not a natural born Python programmer. I am a Zope programmer discovering the heady powerfulness of python.
To give you some more info. I am calling my python script from a form which amongst other things saves values from a multiple select list into Zope object properties. This all works hunkydory. Then I wish to save the values of this multi select into a cookie so that when the user sees the form again the same values are preselected. I appears that even when I save a python list into the cookie as you suggest: ['1','2','3'] the cookie value is always interpreted as a string and not a list. So in my form I cannot loop through the cookie values to select items in the multi select. This my python code: a=['1','2','3','4'] RESPONSE.setCookie('c_lecturer',a) In the request variable I see the cookie set to "['1','2','3','4']" ie as a string. Which is not much use to me. I need to pass the cookie an array. Does this make more sense ? alison Quote:
|
|
#6
|
||||
|
||||
|
No poblem
. I don't know how Zope loads cookies, i've only used it a few times, but i'm assuming by the nature of you're problem the results are being returned as a string something like this.Code:
"a=['1','2','3','4']; b=['1','2','3','4']" In python cgi you can use soemthing like this function to parse the 'HTTP_COOKIE' string into a useable form. Code:
def cookieList(string):
cookies = string.split('; ')
handler = {}
for cookie in cookies:
key, value = cookie.split('=')
if value:
value = value[1:-1].split(',')
handler[key] = value
cookieList("a=['1','2','3','4']; b=['1','2','3','4']")
Of course this all depends on how you're cookie string is returned and the function is pretty basic, could use some building on. I'd still try looping over you're setCookie() calls and adding the values 1 by 1. Just a guess but it seems like a good plan. Hope this helps, Mark. |
|
#7
|
|||
|
|||
|
Thanks
Thanks for your help.
It took me a long time but finally I realised that I was experiencing two problems. The first was that as you correctly suggested I had to reformat my cookie string into a usable list which I did using a python script by stripping quotes and square brackets. But in order to get this far I had to realise that what was really screwing me up was that cookies on IE5.2 for Mac were not working properly. But this problem is another quest for another forum. My program now works on Mozilla and I am looking for an IE work around. Alison Quote:
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > cookies and arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|