#include <time.h>
#include <string>
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include "pcgi.h"
#define gbook_file "../usrtmp/gbook.html"
#define older_gbook_file "../usrtmp/gbookold.html"

int main()
    time_t timeNow;
    string Query;
    string rem_host;
    cin >> Query;
    Query = clean_query(Query);
    string rem_add(getenv("REMOTE_ADDR"));
//not always the remote client sends the REMOTE_HOST env. var., in this case,
//a segmentation fault will occur if 'getenv("REMOTE_HOST")' was assigned 
//to a var.
    if(getenv("REMOTE_HOST") == '\0')
        rem_host.assign("unkown hostname");
    string wagent(getenv("HTTP_USER_AGENT"));

    char *tempstr = new char[1000];
    system("cp " gbook_file older_gbook_file);
    ifstream gb_old(older_gbook_file);
    ofstream gb(gbook_file);
    string stest;
//append a newline '\n' char. as the '\n' char. is not included in the 'stest' 
    gb << stest << endl;
    while(strcmp(stest.c_str(), "<!--ADD GB ENTRY-->")) {
        gb << stest << endl;
//add new guestbook entry:
    gb << "\n<!-- Remote address: " << rem_add << " (" << rem_host << ")"
       << "\n     HTTP client: " << wagent << " -->" << endl;
    gb << "<p><b>Date/time: </b>" << ctime(&timeNow);
    gb << "<br><b>Name: </b>" << cgi_query(Query,"name") 
       << " &nbsp;<b>Nickname: </b>" << cgi_query(Query, "nick") << endl;
    gb << "<br><b>Email: </b><a href=\"mailto:" << cgi_query(Query,"email") 
       << "\">" << cgi_query(Query,"email") << "</a>" 
       << " &nbsp;<b>Country: </b>" << cgi_query(Query, "country") << endl;
    gb << "<br><b>Homepage: </b><a href=\"" << cgi_query(Query,"url") 
       << "\">" << cgi_query(Query,"url") << "</a>" << endl;
    gb << "<br><b>Comments: </b>" << endl;
    gb << "<br>" << cgi_query(Query,"comments") << endl;
//append the old entries after the new one:
    while(!gb_old.eof()) {
        gb << stest;
//the </html> tag is at the end of the file, do not append a newline after it.
        if(stest != "</html>")
            gb << endl;
    cout << "Content-Type: text/html\n\n";
    cout << "<html>\n<head>\n";
    cout << "<title>Thanks for signing our guestbook...</title>\n</head>\n";
    cout << "<body bgcolor=#FFFFFF>\n";
    cout << "<p>Thanks " << cgi_query(Query,"name") 
         << ", for signing our guestbook";
    cout << "\n\n<p>Click <a href=\"/usrtmp/gbook.html\">here</a> to view our" 
         << " guestbook";
    cout << "\n</body>\n</html>";
    return 0;

1. Create 'gbook.html' & 'gbookold.html' files in any directory that can be accessed by your HTTP users, then issue this command:

        chmod 666 gbook*.html

2. Edit the C++ code, and change the '#define' lines to point to indicate appropriate path/gbook.html & path/gbookold.html files that you have created in step 1. Note: make the path relative to the CGI binaries directory and NOT an absolute path.
3. Compile this code into your CGI binaries directory. Note that you need the pcgi.h header file in order to compile, click here to get it.
4. Edit 'gbook.html' and design it in whatever way you wish, but avoid having more than 1000 characters on the same line in the gbook.html file, and be sure to insert this code:
        <!--ADD GB ENTRY-->
at the line where you want the guestbook entries to be inserted.

1. if a line in 'gbook.html'is more than 1000 chars. long -> endless loop somewhere.

To do:
1. Instead of hardcoding the HTML format of "Name", "Email", "Homepage", make them a set of CSS classes so that changing the guestbook style would be flexible & easy.
2. automatically create a new gbook file & backup the old one when size is kinda big.

This code has been written using lpe & was converted to html using code2html.