You are here:

C++/c++/cookies & cgi


cookie problem!

please, perform the following links.

I can see that, if I go from cookies~cookies3.
The cookie values keep increasing.
But if I do
cookies -> cookies2 -> cookies3 -> cookies2 -> cookies

go back....
the values of cookies2 and cookies has not changed and must be refreshed.
once I press the refresh button, everything is the cookies are loaded propery.
Is there anything I can do so that the values can be automatically refreshed? (without refreshing it using html) I'm thinking that the cgi file doesn't reload. All it does is bring up the previously loaded page.

code for cookies.cgi

#include <string>
#include <iostream>
#include <map>

typedef std::map<std::string, std::string> CookieMapType;

using namespace std;

int LoadCookie(CookieMapType&, int);
int WriteCookie(CookieMapType&);
int ViewCookie(CookieMapType&);

int LoadCookie(CookieMapType &cookieData, int login){
//load the cookies
char *tmpCookie = "";
string cookie, cookieName, cookieValue;

if (getenv("HTTP_COOKIE") != NULL)
  tmpCookie = getenv("HTTP_COOKIE");

string cookies = tmpCookie;

if (getenv("HTTP_COOKIE") != NULL)
  cookies += ";";

if (login == 1){
  cookies += "AdminCookie=adminlogin;";

while (cookies.length() > 1)
  cookie = cookies.substr(0, cookies.find(';'));
  if (cookie.substr(0,1) == " ")
     cookie = cookie.substr(1, cookie.length());
  cookieName = cookie.substr(0, cookie.find('='));
  cookieValue = cookie.substr(cookie.find('=')+1, cookie.length());
  cookieData[cookieName] = cookieValue;
  cookies = cookies.substr(cookies.find(';')+1, cookies.length());

//end of load cookies

return 0;

int WriteCookie(CookieMapType &cookieData){

CookieMapType::const_iterator loop;
//write cookies
  cout << "Content-type: text/html\n";
  for (loop = cookieData.begin(); loop != cookieData.end(); loop++)
     cout << "Set-Cookie:" << loop->first << "=" << loop->second << ";\n";
//end of write cookies

cout << "\n<H1>Cookies:</H1>";
cout << "\n<UL>";
for (loop = cookieData.begin(); loop != cookieData.end(); loop++)
  cout << "<LI>" << loop->first << " is " << loop->second;
     if ( loop->first == "AdminCookie" ){
        cout << "hi";
cout << "\n</UL>";

return 0;

int ViewCookie(CookieMapType &cookieData){

CookieMapType::const_iterator loop;
//view cookies
string inpName, inpValue;
int Is_Cookie=0;

for (loop = cookieData.begin(); loop != cookieData.end(); loop++)
     if ( loop->first == "AdminCookie" ){

//end of view cookies
return Is_Cookie;

int main(){
CookieMapType cookieData;


  cout << "<HTML>";
  cout << "<BODY>";

  cout << "</BODY>";
  cout << "</HTML>";

return 0;

This is not a C++ question. As far as I remember, there is way (maybe using java script), to authomatically refresh the html document priodically. This cannot be handled in server side. It should be handled in client side (in browser). Ask a web expert to help you.


All Answers

Answers by Expert:

Ask Experts




You may ask me about simple to complicated C/C++ programming methods/style and most like questions about network programming in C++.


I'm a professional programer in C++ and network/internet programming.

©2017 All rights reserved.