Table of Contents
Implementation
by Peter Foti, 2002/02/04
We explain here how to manipulate cookies with:
- server side functions for getting the cookie.
- client side javascript code for setting, getting and deleting the cookies.
Some of this code is almost a duplication of the websGetRequestDir function.
The GoAhead webserver supports cookies, but there are no built-in methods for accessing them.
Perhaps this can be added to a future release (along with even more cookie support)?
Limitations
I have no way of returning the value of 1 specified cookie name.
For example, if the cookie string looked like:
"name=value; name2=value; name3=value;"
and you wanted the value of name2… still no way to do that).
Webs (ASP)
Add the following functions to the webserver code:
webs.h
extern char_t *websGetCookies(webs_t wp);
webs.c
/******************************************************************************/ /* * Get the cookies for this request */ char_t *websGetCookies(webs_t wp) { a_assert(websValid(wp)); if (wp->cookie == NULL) { return T(""); } return wp->cookie; }
main.c
/****************************** Forward Declarations **************************/ static int getCookie(int eid, webs_t wp, int argc, char_t **argv); websAspDefine(T("getCookie"), getCookie); /******************************************************************************/ /* * Test Javascript binding for ASP. This will be invoked when "getCookie" is * embedded in an ASP page. See web/cookie.asp for usage. Set browser to * "localhost/cookie.asp" to test. */ int getCookie(int eid, webs_t wp, int argc, char_t **argv) { char_t* name; char_t* str; int retval = -1; if (ejArgs(argc, argv, T("%s"), &name) >= 1) { /* Copy the cookie string so we dont trash it */ fmtAlloc(&str, BUF_MAX, "%s", websGetCookies(wp)); /* this is the interesting line -- we return our value to the interpreter instead of to the browser */ ejSetResult(eid, str); bfree(B_L, str); retval = 0; } else { /* * !!! Add error handling support! */ } return retval; }
Example
Here is an example, cookie.asp:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content='text/html; charset="UTF-8"'/>
<title>Cookie Test Page</title>
<style type="text/css">
.clientside
{
border: 1px black solid;
background-color: #eee;
color: black;
margin: 5px;
padding: 4px;
}
.serverside
{
border: 1px black solid;
background-color: #ccc;
color: black;
margin: 5px;
padding: 4px;
}
</style>
<script type="text/javascript">
<!--
// Cookie expires in 10 minutes
expireDate = new Date();
expireDate.setMinutes( expireDate.getMinutes() + 10 );
function setCookie()
{
cookieValue = "";
for ( i = 0; i < document.myform.length; i++)
{
if (document.myform[i].type == "text")
{
document.cookie = document.myform[i].name + "=" + document.myform[i].value + ";expires=" +
expireDate.toGMTString();
}
}
}
function getCookie( cName )
{
if (document.cookie != "")
{
thisCookie = document.cookie.split(";");
for ( i = 0; i < thisCookie.length; i++ )
{
cookieName = thisCookie[i].split("=")[0];
if (cookieName == cName)
return thisCookie[i].split("=")[1];
}
return "";
}
}
function getCookies()
{
if (document.cookie != "")
{
thisCookie = document.cookie.split(";");
for ( i = 0; i < thisCookie.length; i++ )
{
cookieName = thisCookie[i].split("=")[0];
cookieVal = thisCookie[i].split("=")[1];
document.write(cookieName + ": " + cookieVal + "<br \/>");
eval("document.myform." + cookieName + ".value = \"" + cookieVal + "\"");
}
}
else
document.write("No Cookies");
}
function deleteCookie()
{
thisCookie = document.cookie.split(";");
expireDate = new Date;
expireDate.setYear( expireDate.getYear() - 1 );
for ( i = 0; i < thisCookie.length; i++ )
{
cookieName = thisCookie[i].split("=")[0];
document.cookie = cookieName + "=;expires=" + expireDate.toGMTString();
}
}
//-->
</script>
</head>
<body>
<h1> Sample Cookie functions </h1>
<form id="myform" name="myform" action="/goform/login" method="post">
sessionError: <input type="text" name="sessionError" size="40" /><br />
TestCookie: <input type="text" name="TestCookie" size="40" /><br />
<input type="button" onclick="deleteCookie();" value="Delete Cookie"/>
<input type="button" onclick="setCookie();" value="Update Cookies"/>
</form>
<div class="clientside">
<h2>Client Side:</h2>
<script type="text/javascript">
<!--
getCookies();
//-->
</script>
</div>
<div class="serverside">
<h2>Server Side:</h2>
<%
// Someday, would like to get back only the value for the cookie name specified
val = getCookie("sessionError");
write(val);
%>
</div>
</body>
</html>