Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    885
    Thanks
    173
    Thanked 101 Times in 101 Posts

    cookie -- learning to read problem

    Hi all



    How should I be doing this?

    I am trying to 'learn' how to set more than one value in a cookie.
    Then get them back out. Obviously i'm on the wrong track.

    These are my 'test' values:
    Code:
    var val = document.getElementById("yrname").value; //peterPan
    var val1 = document.getElementById("password").value; //tinkerBell
    //send to create cookie factory
    createCookie("inbed", val + '~' + val1, "1");
    Then from url : alert(cookie)

    I get:
    inbed=peterPan~tinkerBell

    So far so good (well what I expected anyway)

    now using this function I completely lose tinkerBell.
    she was inbed with peterPan.


    Code:
    function readCookie(name) //name is passed in
    {  
        var cookies = document.cookie.split(/~/g); //split at ~
    		//alert(cookies);
    		//results:
    		//cookies: inbed=peterPan,tinkerBell
    		//cookies[0]: mytestcookie=peterPan
    		//cookies[1]: tinkerBell //the last sighting		
        var arg = name + "=";  
        for ( var c = 0; c < cookies.length; ++c ) //iterate through the cookie
        {			
            var ck = cookies[c]; //cookie array into an var
    	//alert(ck); //inbed=peterPan (no tinkerBell -- not what I expected!
            
    	//basically i'm now lost!!!
    	//I don't get what happened to tinkerBell.
    				
    	if ( ck.indexOf(arg) == 0 ) 
    //if the array at the index of arg 'name=' equal to position 0.
            {
                var temp = ck.split(/=/); //split the  cookie array at =
    	//alert(temp); //inbed=peterPan
    	//alert(temp[0]); //inbed
    	//alert(temp[1]); //peterPan
    							,
                return decodeURI(temp[1]) //bring back array 1
            }
        }
        return "";
    }
    help on how I should
    be doing this
    much appreciated

    LT
    Last edited by low tech; 07-13-2011 at 12:10 PM.

  • #2
    Regular Coder
    Join Date
    May 2010
    Location
    Bathurst, Australia
    Posts
    180
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Code:
    for ( var c = 0; c <= cookies.length; ++c )
    Silly boy :P
    Also, I recommend you look into the for in loop.
    Disclaimer: I'm hungover 70% of the time i'm on here, any information given may not be correct, or even legible.

  • Users who have thanked BulletTimeBill for this post:

    low tech (07-13-2011)

  • #3
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    885
    Thanks
    173
    Thanked 101 Times in 101 Posts
    Hi BulletTimeBill


    Thanks --- but still no closer --- can you expand on your clue?


    LT

  • #4
    New Coder
    Join Date
    Aug 2005
    Posts
    71
    Thanks
    9
    Thanked 3 Times in 3 Posts
    not intending to butt-in, but he means you're not looking far enough in your array. you put
    Code:
    c < cookies.length
    and the recommendation is
    Code:
    c <= cookies.length
    there is a very subtle difference between the two. you could also do this
    Code:
    c < cookies.length + 1
    hope this helps.
    It's OK to pee against the wind if your standing in a lake.

  • Users who have thanked Budreaux for this post:

    low tech (07-13-2011)

  • #5
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    885
    Thanks
    173
    Thanked 101 Times in 101 Posts
    Hi Budreaux


    not intending to butt-in, but he means you're not looking far enough in your array.
    You're not butting in ---- all opinions are very welcome and appreciated

    Today is a new day and your explanation of not looking deeper into the array was, for me, very helpful ---- because I never considered it. So i'll try that and see what happens.

    I'm only experimenting and trying to learn more about cookies --- in this case adding multiple values and getting them back out.

    I'm def not sure how this "should" be done.

    Thanks

    LT
    Last edited by low tech; 07-13-2011 at 11:54 PM.

  • #6
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Budreaux View Post
    not intending to butt-in, but he means you're not looking far enough in your array. you put
    Code:
    c < cookies.length
    and the recommendation is
    Code:
    c <= cookies.length
    That's back to front. Originally it was c <= cookies.length and that is wrong.

    Code:
    var cookies = document.cookie.split(/~/g);
    Assuming there are no other cookies on the domain, doing that will produce [ "inbed=peterPan", "tinkerBell" ]. If there are any other cookies they'll probably end up as part of either element.

    Use a standard cookie reader to get the entire value of the wanted cookie, then split it on ~.

  • Users who have thanked Logic Ali for this post:

    low tech (07-14-2011)

  • #7
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    885
    Thanks
    173
    Thanked 101 Times in 101 Posts
    Hi Logi Ali

    No, no other cookies --- just this experiment for the purpose of learning.

    anyway

    Use a standard cookie reader to get the entire value of the wanted cookie, then split it on ~.
    I thought that is what I was attempting to do hahaha obviously not haha


    I have cookie examples of setting a single cookie with name/value and I can do that and get it back.

    But I have read in JSB (js bible) and other places something 'similar' to this


    A cookie value can be made up of several or many elements with a delimiter ( eg | ).
    Example:
    var cookieValue = name + "|" + pass

    and when the cookie is recovered use .split("|")
    to break it back into the individual variable values.

    I can't (or haven't been able to) find a good example of this so I thought i'd try anyway.

    Set ONE cookie with both username and password (of course not for real) values.

    then get them back out and into form.

    However, I admit i'm not doing well hahahhaha

    I'm just not clear why I would need to set two cookies -- one for username, one for password IF it can be done in one cookie?

    Anyway

    Thanks

    LT
    it's all fun.
    Last edited by low tech; 07-14-2011 at 04:13 AM.

  • #8
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    885
    Thanks
    173
    Thanked 101 Times in 101 Posts
    Hi all

    Well for the benefit of others who may also find a problem in grasping this concept, I found that this description sums up what I am trying to do

    Separate mutiple descriptors and values in a single cookie using the 'split()' method.
    from THIS SITE which also provides a better example than anything I've been able to find so far.

    A step in the right direction I feel.

    LT

  • #9
    Banned
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A cookie, also known as an HTTP cookie, web cookie, or browser cookie, is used for an origin website to send state information to a user's browser and for the browser to return the state information to the origin site.

    <%@ page language="java" import="java.util.*"%>
    <%
    String username=request.getParameter("username");
    if(username==null) username="";


    Date now = new Date();
    String timestamp = now.toString();
    Cookie cookie = new Cookie ("username",username);
    cookie.setMaxAge(365 * 24 * 60 * 60);
    response.addCookie(cookie);

    %>
    Last edited by WA; 07-14-2011 at 10:30 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •