Creating & Viewing Cookies in CGI using C++.

Creating Cookie in CGI Using C++ :

Here im going to create 4 cookies.


#include <iostream>
using namespace std;

int main ()
{
 //cookie 1 with name UserID & value Praneeth
   cout << "Set-Cookie:UserID=Praneeth;\r\n";
 //cookie 2 with name Password & value XYZ123
   cout << "Set-Cookie:Password=XYZ123;\r\n";
 //cookie 3 with name Domain & value www.tutorialspoint.com
   cout << "Set-Cookie:Domain=www.tutorialspoint.com;\r\n";
 //cookie 4 with name Path & value perl
   cout << "Set-Cookie:Path=/perl;\n";
   cout << "Content-type:text/html\r\n\r\n";
   cout << "<html>\n";
   cout << "<head>\n";
   cout << "<title>Cookies in CGI</title>\n";
   cout << "</head>\n";
   cout << "<body>\n";

   cout << "Setting cookies" << endl;

   cout << "<br/>\n";
   cout << "</body>\n";
   cout << "</html>\n";
 
   return 0;
}


Now im Retrieving the Cookies as a list from web Browser.

#include <iostream>
#include <vector>  
#include <string>  
#include <stdio.h>  
#include <stdlib.h> 

#include "cgicc/CgiDefs.h" 
#include "cgicc/Cgicc.h" 
#include "cgicc/HTTPHTMLHeader.h" 
#include "cgicc/HTMLClasses.h"

using namespace std;
using namespace cgicc;

int main ()
{
   Cgicc cgi;
   const_cookie_iterator cci;

   cout << "Content-type:text/html\r\n\r\n";
   cout << "<html>\n";
   cout << "<head>\n";
   cout << "<title>Cookies in CGI New</title>\n";
   cout << "</head>\n";
   cout << "<body>\n";
   cout << "<table border = \"0\" cellspacing = \"2\">";
   
   // get environment variables
   const CgiEnvironment& env = cgi.getEnvironment();

   for( cci = env.getCookieList().begin();
        cci != env.getCookieList().end(); 
        ++cci )
   {
      cout << "<tr><td>" << cci->getName() << "</td><td>";
      cout << cci->getValue();                                 
      cout << "</td></tr>\n";
  }
   
   cout << "</table>\n";
  
   cout << "<br/>\n";
   cout << "</body>\n";
   cout << "</html>\n";
   
   return 0;
}


Second Type retrival :
 Here i need only to check what is the value for UserID so the code for that is 


#include <iostream>
#include <vector>  
#include <string>  
#include <stdio.h>  
#include <stdlib.h> 

#include "cgicc/CgiDefs.h" 
#include "cgicc/Cgicc.h" 
#include "cgicc/HTTPHTMLHeader.h" 
#include "cgicc/HTMLClasses.h"

using namespace std;
using namespace cgicc;

int main ()
{
   Cgicc cgi;
   const_cookie_iterator cci;
   cout << "Content-type:text/html\r\n\r\n";
   cout << "<html>\n";
   cout << "<head>\n";
   cout << "<title>Cookies in CGI New</title>\n";
   cout << "</head>\n";
   cout << "<body>\n";
   cout << "<table border = \"0\" cellspacing = \"2\">";
   // get environment variables
   const CgiEnvironment& env = cgi.getEnvironment();
   for( cci = env.getCookieList().begin(); cci != env.getCookieList().end();  ++cci )
   {
string cooknam  = cci->getName();
string cookvalu = cci->getValue();
if (cooknam == "UserID")
{
   cout << "<tr><td>Value of UserID cookie is <td> " << cookvalu << "</tr>";                                 
    }
   cout << "</table>\n";
   cout << "<br/>\n";
   cout << "</body>\n";
   cout << "</html>\n";
   return 0;
}

Comments