Persistent CGI Program Example

The following example shows a counter that is increased each time the Persistent

CGI program is called.

/***********************************************************************/
/ This is a sample Persistent CGI program */
/ This program is invoked by a URL */
/ http://hostname/cgi-bin/samplePersistent.pgm?bin=1) */
/***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 1024
unsigned int MAXINPUT = BUFSIZE; /* Maximum input data.*/
int count=1;
int main()
{
char *pt;
char carac[2]=" ";
int bin=1;
freopen("", "r", stdin); /* You need to re-open the stdin for the Persistent CGI Program
pt=getenv("QUERY_STRING");
carac[0]=pt[4];
bin=atoi(carac);
if(bin == 1)
{
printf("Accept-HTSession: webpg101101 \n");
printf("Content-type: text/html \n\n");
printf("<html><title>Test persistent CGI</title><body>");
printf("<h2> The first form</h2>");
printf("<form action=\"/cgi-bin/webpg101.pgm/webpg101101\" ");
printf("method=\"GET\"> ");
printf("<input type=HIDDEN NAME=BIN VALUE=2>");
printf("<input type=reset value=Reset>");
printf("</form></body></html>");
count++;
}
if(bin == 2)
{
pt=getenv("QUERY_STRING");
printf("Accept-HTSession: webpg101101 \n");
printf("Content-type: text/html \n\n");
printf("<html><title> Test persistent CGI</title><body>");
printf("<h2> The second form</h2>");
printf(" Valor count: %i",count);
printf("Query string: %s",pt);
printf("<form action=\"/cgi-bin/webpg101.pgm/webpg101101\" ");
printf("method=\"GET\"> ");
printf("<input type=HIDDEN NAME=BIN VALUE=3>");
printf("<h2>Persisten CGI si funcionan</h2>");
printf("<input type=submit value=Execute>");
printf("</form></body></html>");
count++;
}
if(bin == 3)
{
printf("Accept-HTSession: webpg101101 \n");
printf("Content-type: text/html \n\n");
printf("<html><title> Test persistent CGI</title><body>");
printf("<h2> The third form</h2>");
Chapter4. Using Persistent CGI Programs 83