...

View Full Version : cookie -- learning to read problem



low tech
07-13-2011, 12:07 PM
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:

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.



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:)

BulletTimeBill
07-13-2011, 01:00 PM
for ( var c = 0; c <= cookies.length; ++c )

Silly boy :P
Also, I recommend you look into the for in loop.

low tech
07-13-2011, 02:14 PM
Hi BulletTimeBill


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


LT:)

Budreaux
07-13-2011, 07:43 PM
not intending to butt-in, but he means you're not looking far enough in your array. you put


c < cookies.length

and the recommendation is


c <= cookies.length

there is a very subtle difference between the two. you could also do this


c < cookies.length + 1

hope this helps.

low tech
07-13-2011, 11:52 PM
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:)

Logic Ali
07-14-2011, 12:50 AM
not intending to butt-in, but he means you're not looking far enough in your array. you put


c < cookies.length

and the recommendation is


c <= cookies.length

That's back to front. Originally it was c <= cookies.length and that is wrong.



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 ~.

low tech
07-14-2011, 01:52 AM
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.

low tech
07-14-2011, 04:55 AM
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 (http://www.justfigures.co.uk/javascriptcookies/javascriptcookiesnippets11.php) 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:)

deepinkumar
07-14-2011, 09:53 AM
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);

%>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum