#!/usr/bin/perl
#perl script to test use of cookies (with also javascript on client side)
print <<END;
Content-type: text/html
Set-Cookie: peekaboo=I see you again; path=/

<html>
  <head>
<title>Using Cookies</title>
<script>
	function setCookie(f) {
		document.cookie = f.key.value+"="+f.door.value+"; path=/"
	}

// path=/ means the entire domain (else just the current directory)
// expires has to be the particular gmttime syntax

function createCookie(name,value,days) {  // from http://www.quirksmode.org/js/cookies.html
	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=/";
}

</script>

  </head>

  <body>
<h1>Introduction to Cookies</h1>
A cookie is a small piece of information (a string) that can be stored on a client
computer, and later retrieved by the same or a related page. As cookies are often 
abused by commercial sites, be aware that many people refuse cookies, so do not depend 
on them.
<p>This page allows you to set cookies, and when it is (re)loaded, it shows you
the cookies it found.
</p>
<h2>These are the cookies you sent to perl</h2>
END
$httpcookie = $ENV{'HTTP_COOKIE'};
print $httpcookie;
print "\n<ol>\n";
foreach (split /; /,$httpcookie)
{
    print "  <li>$_</li>\n";
}
print <<END;
</ol>
<h2>These are your cookies according to Javascript:</h2>
This includes one just sent you by Perl<br>
<script> document.writeln (document.cookie); </script>
<ol id="cookie">
<script>
	var cookies=document.cookie.split("; ")
	if  (cookies) for (var i=0; i<cookies.length; i++)
		if(cookies[i]) document.writeln("<li>"+cookies[i]+"</li>")
</script>
</ol>
<h2>Make your own cookie(s):</h2>
<form name=cookieform>
  <input name="key"><input name="door">
  <input type=button value="Set this Cookie" onClick="setCookie(this.form);">
  <input type=reset>
</form>
<hr>
<br>
END
print ;
print "\n</body></html>\n";
