Cooky.c

This code is executed when the Client clicks on the SUBMIT FORM button provided on the Registration form.

Source code of Cooky.c

#include <stdio.h>

#include "cgic.h"
#include <windows.h>
#include <string.h>
#include <sql.h>
#include <sqlext.h>
#include <dos.h>
#include <ctype.h>

//POINTER DECLARATIONS FOR ODBC CONNECTION
void *henv;
void *hdbc;
void *hstmt;

char *Cityfield[] = { "Mumbai","Delhi", "Chennai","Calcutta"};
int CityChoice;
char *mode[] = { "Cash","Cash", "Cash"};
int ModeChoice;
char *idno;
char idno1[10];
int ii=0;

char a[100];char aa[100];char bb[100];int qq;
int DATAresult,EMAILresult,result;

// FUNCTION PROTOTYPES
void Username();
void Useraddr();
void Useraddr1();
void Sity();
void Zipcode();
void Payment();
void Mailcode1();
void Mailcode2();
char *identity();

//BUFFER DECLARATION
char username[30];
char useraddr[50];
char useraddr1[50];
char zipcode[8];
char mailcode1[15];
char mailcode2[15];


int cgiMain()
{
char extra[30]="s=";
// function call
Username();
Useraddr();
Useraddr1();
Sity();
Zipcode();
Payment();
Mailcode1();
Mailcode2();

// odbc code starts
SQLAllocEnv(&henv);
SQLAllocConnect(henv,&hdbc);
SQLConnect(hdbc,"RAVI",-3,0,0,0,0);


// email CORRECTIONS

SQLAllocStmt(hdbc,&hstmt);
sprintf(aa,"SELECT * FROM R WHERE EMAIL='%s' and EMAIL1='%s'",&mailcode1,&mailcode2);
SQLExecDirect(hstmt,aa, -3);
EMAILresult=SQLFetch(hstmt);
SQLFreeStmt(hstmt,SQL_DROP); // SQLFreeStmt 1


// checking for unique data

SQLAllocStmt(hdbc,&hstmt);
sprintf(aa,"SELECT NAME FROM R WHERE NAME='%s' and ADDR='%s' and ADDR1='%s' and CITY='%s' and ZIPCODE='%s' and PAYMENT='%s' and EMAIL='%s' and EMAIL1='%s'",&username,&useraddr,&useraddr1,Cityfield[CityChoice],&zipcode,mode[ModeChoice],&mailcode1,&mailcode2);
SQLExecDirect(hstmt,aa, -3);
DATAresult=SQLFetch(hstmt);
SQLFreeStmt(hstmt,SQL_DROP); // SQLFreeStmt 2


// NESTED IF LOOP
if (DATAresult != 0 && EMAILresult != 0)
{
idno=identity();
strcpy(idno1,idno);
strcat(extra,idno1);
SQLAllocStmt(hdbc,&hstmt);
sprintf(aa,"INSERT INTO R(IDNO,NAME,ADDR,ADDR1,CITY,ZIPCODE,PAYMENT,EMAIL,EMAIL1) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s')",&extra,&username,&useraddr,&useraddr1,Cityfield[CityChoice],&zipcode,mode[ModeChoice],&mailcode1,&mailcode2);
SQLExecDirect(hstmt,aa, -3);
SQLFreeStmt(hstmt,SQL_DROP); // SQLFreeStmt 3
ii=1;
}

SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);


if (ii==1)
{
fprintf(cgiOut, "set-cookie:s=%s;path=/cgi-shl/;\n",idno1);
}
cgiHeaderContentType("text/html");
fprintf(cgiOut, "<HTML><HEAD>\n");
fprintf(cgiOut, "<TITLE>Registered data</TITLE></HEAD>\n");
fprintf(cgiOut, "<BODY><H1>REGISTRATION PROCESS</H1>\n");

fprintf(cgiOut, "<B>Name :</B> %s.<BR>\n",username);
fprintf(cgiOut, "<B>Address :</B> %s", useraddr);
fprintf(cgiOut," ,%s.<BR>\n", useraddr1);
fprintf(cgiOut, "<B>City : </B>%s.<BR>\n", Cityfield[CityChoice]);
fprintf(cgiOut, "<B>Zip :</B>%s.<BR>\n", zipcode);
fprintf(cgiOut, "<B>Payment : </B>By %s.<BR>\n", mode[ModeChoice]);
fprintf(cgiOut, "<B>E-Mail: </B>%s<B>@</B> ", mailcode1);
fprintf(cgiOut," %s \n", mailcode2);
fprintf(cgiOut,"<BR><BR><BR>\n");

if (DATAresult == 0)
{
fprintf(cgiOut, "<B>you have already been registered with the above data </B>\n");
}
else

if (EMAILresult == 0)
fprintf(cgiOut, "<B>EMAIL ID ALREADY TAKEN </B>\n");

else

{

fprintf(cgiOut, "YOUR ARE REGISTERED WITH ID NO %s<BR><BR>",idno1);
fprintf(cgiOut, "<a href='/listof.htm'>CLICK ON ME FOR SHOPPING </a> ");

}

fprintf(cgiOut, "</BODY></HTML>\n");
return 0;
}// END OF MAIN


void Username()
{
int len,i;
cgiFormStringNoNewlines("username", username, 30);
len=strlen(username);
for(i=0;i<len;i++)
{
username[i]=tolower(username[i]);
}
}

void Useraddr()
{
int len,i;
cgiFormString("useraddr", useraddr, 50);
len=strlen(useraddr);
for(i=0;i<len;i++)
{
useraddr[i]=tolower(useraddr[i]);
}
}

void Useraddr1()
{
int len,i;
cgiFormString("useraddr1", useraddr1, 50);
len=strlen(useraddr1);
for(i=0;i<len;i++)
{
useraddr1[i]=tolower(useraddr1[i]);
}
}

void Sity()
{
cgiFormSelectSingle("city",Cityfield, 4, &CityChoice, 0);
}
void Zipcode()
{
cgiFormString("zip", zipcode, 8);
}
void Payment()
{
cgiFormSelectSingle("mop",mode, 3, &ModeChoice, 0);
}

void Mailcode1()
{
cgiFormString("mail", mailcode1, 15);
}

void Mailcode2()
{
cgiFormString("serve", mailcode2, 15);

}

char *identity()
{
int i;
long j,k,l;
char aa[10];
char bb[10];
FILE *fp;
i=j=k=l=0;
find: fp=fopen("c:\\project\\idno.txt","r+w+b");
if(fp==NULL)
{
goto find;
}
while((i=fgetc(fp))!=-1)
{
if(i==10)
{
bb[j]='\0';
break;
}
bb[j++]=i;
}
bb[j]='\0';
k=atol(bb);
l=k+1;
ltoa(l,aa,10);
fseek(fp,0,0);
fputs(aa,fp);
fclose(fp);
return bb;

}

PROGRAM EXPLANATION :Firstly, you have to add cgic.c and cgic.h to your project workspace in Visual C++. These files have been provided in the downloaded files link . cgic.c performs the following tasks:

The program begins with a few global variable declarations.Cgic.c ,first, parses form data and then calls cgiMain ( ).Here the user defined functions execute the cgi functions which redirects the form input data to our storage buffers.

cgiFormStringNoNewlines("username", username, 30);

The above CGI function redirects the data present in the Form field ,"username"(1st parameter), in the registration form ,to our buffer ,username(2nd parameter).The 3rd parameter indicates the size of the buffer.These buffers are redirected to the Client's browser in an HTML format , using the pointer cgiOut ,which points to the Standard output.

fprintf(cgiOut, "<B>Name :</B> %s.<BR>\n",username);

After the buffers are filled with various form fields , an ODBC connection has to be established with the database ( R.dbf) using the ODBC API functions .The setting can be done in the Control Panel.This has been explained before,in the theoretical approach session .Before the client data is to be INSERTED into the database ,we have to check for the uniqueness of the whole data and also for the uniqueness of the e-mail ID.These error checks have been provided in the program . If a match is found ,then the client is intimidated about the same. If not ,a cookie ,which contains the client's current session ID number , is placed on the Client's machine.Also,the form data is stored into database ,along with his ID number(cookie no.) .The identity function ( ) of the program generates the unique cookie number for every client.

The ODBC functions can be explained as follows :

SQLAllocEnv(&henv);
SQLAllocConnect(henv,&hdbc);
SQLConnect(hdbc,"RAVI",-3,0,0,0,0);

The ODBC environment and call-level interface (CLI) must be initialised before use . The SQLAllocEnv function initialises the environment , allocates necessary memory and returns a handle to this environment . There are two primary calls that are used to connect to an ODBC data source. The SQLAllocConnect accepts handle from the environment and provides a handle to the Database Connectivity. The SQLConnect function is used to make a simle connection to a data source."RAVI" is the Data Source Name (DSN) which has to be provided as the second parameter of this function.

SQLAllocStmt(hdbc,&hstmt);
sprintf(aa,"SELECT * FROM R WHERE EMAIL='%s' and EMAIL1='%s'",&mailcode1,&mailcode2);
SQLExecDirect(hstmt,aa, -3);
SQLFreeStmt(hstmt,SQL_DROP);

The SQLAllocStmt function allocates memory for the statement information and associates the statement with the connection specified by hdbc .It accepts the connection handle ,hdbc , as input and returns a pointer to the new statement handle just created.The SQLExecDirect function will execute a preparable statement contained in the array,aa .SQLFreeStmt is used to perform the following :

SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);

The SQLDisconnect function is typically called after calling the SQLFreeStmt .This function will free all existing hstmts.The SQLFreeConnect function releases the connection handle and frees all memory associated with that handle .The SQLFreeEnv function releases the environment handle and frees all memory associated with that handle .

 

BACK TO MAIN PAGE

cooky1.c:

Recall.c :

Delete.c :

Update.c :

Invoice.c :

Back to Homepage