Client side cookie, multiple key values pairs

I need to have this setup as a client side cookie (javascript disabled - no problem).

I need to store in the cookie page name, first name and last name (and there are about 4 more items). Should i save a different cookie for each item here (all javascript examples show this) or can i do some thing like this?

Response.Cookies(“user”)(“pagename”) = “Site Home”
Response.Cookies(“user”)(“firstname”) = “John”
Response.Cookies(“user”)(“lastname”) = “Smith”
Response.Cookies (“user”).Expires = DATE + 365

Thanks

Since a cookie name and value are just strings, yes you can create some type format that your software can interpret specially and extract the needed parts. The browser doesn’t know or care, all it sees is name=value strings, and it will happily pass them along as is.

Your programming language might have something like this built in already.

Are you referring to some thing like this:

“user=Site Home|John|Smith”

Ya, some way of storing multiple key/value pairs in a string. Don’t reinvent the wheel.

The following functions are good standard functions for processing cookies.


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

It should be trivial to check if a cookie value contains the | symbol, and then to modify the value in an appropriate manner.

Thank you.