#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    4
    Rep Power
    0

    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. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    4
    Rep Power
    0

    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

    Originally Posted by netytan
    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.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    4
    Rep Power
    0
    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


    Originally Posted by netytan
    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.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    4
    Rep Power
    0

    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



    Originally Posted by netytan
    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.

Similar Threads

  1. Replies: 1
    Last Post: December 24th, 2001, 04:25 PM
  2. problems with cookies and arrays
    By ajax in forum PHP Development
    Replies: 2
    Last Post: October 19th, 2001, 11:32 AM
  3. passing arrays in urls
    By how-do-i in forum PHP Development
    Replies: 6
    Last Post: October 16th, 2001, 09:28 AM
  4. Shopping Cart
    By Nash in forum PHP Development
    Replies: 10
    Last Post: June 20th, 2001, 06:12 PM
  5. ModPerl + Arrays + Session: EEK!
    By Represser in forum Perl Programming
    Replies: 0
    Last Post: June 27th, 2000, 11:37 PM

IMN logo majestic logo threadwatch logo seochat tools logo