Using Cookies

Recall that a cookie is a small piece of information that you ask a browser to remember for you. Normally, PHP is used to set cookies, see Using Cookies

JavaScript

Since cookies are actually stored on the client's computer, JavaScript can access them, and the access is immediate, rather than delayed. There is less support for cookies in JavaScript, and access is through the property document.cookie, which behaves rather strangely.
Note: PHP and JavaScript do different things with spaces in either name or value parts of a cookie. Be prepared.

Whether a cookie is set by PHP or Javascript makes no further difference in how it behaves. All cookies in the domain of a server will be sent to that server on all subsequent requests to that server. In this way they maintain a "state" of that client-server interaction

Code for showing cookies

To find a particular cookie, you can use a Regular Expression to look for the pattern of name=value, where the value ends with a ; or the end of document.cookie. In this example, use any var name you wish, and where I wrote cookname, put in the name of the particular cookie you are looking for . (I would avoid using spaces or special characters in cookie names.) m[0] is the complete name=value string, m[1] just the value (after the =)
Caution: If you had cookies named both "firstname" and "me", both would match "me" so you wouldn't know which value you got. (see below)

var cookval

var m = /cookname\=([^\;]+)/.exec(document.cookie)
if (m) 
    cookval = m[1]	// the part in the ( )
else
/* -- that cookie isn't there ---*/

If you are expecting lots of cookies, you could use my function cookies_array (it "splits" the cookie string twice) and a version of the for loop that is similar to PHP's foreach

function cookies_array ()   // returns array like php's $_COOKIE, with existing cookies
{ //author Lin Jensen
var cookies=document.cookie.split("; ")
var a = new Array()
if (cookies[0]) // no cookies still gives one empty string
for (i in cookies) // i indexes the array
{
var c = cookies[i].split("=");
a[c[0]] = c[1]
}
return a;
}

var cook = cookies_array()
for (key in cook)
{document.writeln("<li>"+key+'='+cook[key]+'</li>');

There is however a problem in determining whether a cookie is present, code for this varies from browser to browser.
To find a particular cookie, a RegExp can be used:
// using RegExp: look for cookie name,=, everything up to ; (or end)

var m = /name\=([^\;]+)/i.exec(document.cookie)

if (m) document.write("Welcome back "+m[1]+"<br>")

This first looks for a cookie ending with name, in this example, followed by = and then one or more characters other than a ; the () capture all those characters.
var m is either: