[Top] [Prev] [Next] [Bottom] [Contents]

Example: Cookies and the State Server

This example shows you a very simple application for creating and modifying cookies from the Sapphire/Web State Server. As explained above, the State Server can retain cookie values for you, holding them by an automatically generated number. Note that this example is here for historical purposes only. It is much easier to use SaGetStateVar() and SaSetStateVar().

To operate this, you first request a cookie, and then write into it. Later on you can retrieve it from the State Server by specifying the number. Finally, you can delete the cookie. The cookie ID number assigned by Sapphire/Web has no meaning, and can therefore be used as a safeguard against prying eyes.

For further information on the Sapphire/Web API calls used here, please refer to the Sapphire/Web Reference Manual, Chapter 2.

You may find that you can use a state server installation on another machine successfully, but not one on your own machine.

Overview

Here is what the continuation of this example - with all the code on it - is intended to do.

When you press Submit, the listed cookie number you supplied will be queried for a state server value. If it is found, the "String to Append" value will be appended to the value of that cookie and re-saved. If it is not found, 2 things can happen. If a String to Append is given, a cookie will be created with your String to Append as the saved value. The cookie number created depends on your input cookie number. If it is 0 or invalid (text), then the next available cookie will be retrieved. If it is >0 and valid, then a cookie will be created with that number. If no String to Append is given, no cookie will be created, and you will be notified that no such cookie exists.

If you do not have the State Server running you will get a cookie value of minus one.

In the following sections you add code to your project. If you see the following lines already present in the code area you are modifying, do not place your code between them, as it will be lost.

/***** Begin Site Registry Code  *****/
/***** End of Site Registry Code *****/
Sapphire/Web considers that it "owns" any code between these two comments, and that it has the "right" to alter it as it sees fit.

Creating the Project and HTML

1. Create a new Sapphire/Web project, calling it StateTest.

2. Create a new HTML document, leaving it the default name home.html.

3. In home.html add the following HTML code:
<A HREF="URL" >Go to Cookie reviewer</A>
4. Create a new HTML document, naming it page2.html.

5. In page2.html add the following HTML code:
Cookie Number = ##Sa_cooknum##
Cookie Value  = ##Sa_cookval##
<BR><P>
<FORM ACTION="FormAction" METHOD="post" >
Cookie Number? : <INPUT NAME="GetCookNum" VALUE="" 
SIZE=20><BR>
String to Append? : <INPUT NAME="GetAppendage" VALUE="" 
SIZE=20><BR>
<INPUT TYPE="SUBMIT" VALUE="Submit"><INPUT TYPE="RESET" 
VALUE="Reset">
<INPUT TYPE="hidden" NAME="SaFormName" 
VALUE="FormAction__Fpage2_html"> </FORM>
6. Bind the activator URL on home.html with a null binding to page2.html.

7. Bind the form activator FormAction on page2.html to page2.html.

Adding Code to your Project

This project needs code in order to do what we want of it.

Step 1 - right-click the URL activator for home.html and select Code.

Includes

Under "#Includes, #defines, globals, and other code" add the following code:

#include "sacook.h"
int cooknum;
char *g_cooknum, *g_cookval;
char buff[100];
char *tmp;

Before Code

Under "Before Object binding Execution code" add the following code:

SaRegisterStringSite("page2.html", "cooknum", "N/A");
SaRegisterStringSite("page2.html", "cookval", "N/A");
Save and close the code window.

Step 2 - Right-click the FormAction activator for page2.html and select Code.

Includes

Under "#Includes, #defines, globals, and other code" add the following code:

#include "sacook.h"
int cooknum;
char *s_cooknum, *s_cookval;
char buff[100];
char *tmp, *tmp2;

Before Code

Under "Before Object binding Execution code" add the following code:

tmp = SaGetInputValue("GetCookNum");
tmp2 = SaGetInputValue("GetAppendage");
cooknum = atoi(tmp);
sprintf(buff, "%d",cooknum);
s_cooknum = SaCopyString(buff);
s_cookval = SaGetCookie(cooknum);
if (!s_cookval)
{
	if (strlen(tmp2) > 0)
	{
		if (cooknum > 0)
			SaInternStringCookie(tmp2, cooknum);
		else
			cooknum = SaInternString(tmp2);
		s_cookval = tmp2;
	}
else
	{		
	s_cookval = "Cookie Not Set!";
	sprintf(buff, "%d",cooknum);
	s_cooknum = SaCopyString(buff);
	}
}
else
{
	s_cookval = SaStrMCat(s_cookval, tmp2, NULL);
	SaUpdateCookie(s_cookval, cooknum);
}

SaRegisterStringSite("page2.html", "cooknum", s_cooknum);
SaRegisterStringSite("page2.html", "cookval", s_cookval);
Save your code, save the project, and test it.

Testing StateTest

Click on "Go to the Cookie Reviewer", and the screen shows you this:

Click on the Submit button. This will submit a query for a non-existent cookie number (blank). You will get this:

The screen above shows that the code has submitted a request for a cookie, found that no value was associated with that cookie, and that no cookie number had been asked for in the first place, so stored the string and received a cookie number back. This should be the next available number within the state server.

This time, put the example value "qwerty" into the "String to Append" type-in box and press Submit.

Append a string to the content of that cookie. Enter the number returned as the cookie ID (400000 in the instance above), and specify the concatenation of "uiop". You'll see the cookie number used and the concatenated string.



[Top] [Prev] [Next] [Bottom] [Contents]

info@bluestone.com
Copyright © 1997, Bluestone. All rights reserved.