The following step by step procudure describes an experiment that demonstates how to set up a Web site and a CGI script that can be run off it. It is assumed that a user has a Unix or linux account on a system that runs Apache-compatible Web server with local user defined configuration files and CGI scripting enabled.
mkdir public_html chmod a+x,go-rw . public_html cd public_html mkdir cgi-bin chmod a+x,go-rw cgi-bin cd cgi-bin
Options +ExecCGI addType application/x-httpd-cgi cgi pl DirectoryIndex index.html index.shtm Options +Includes # uncomment for Apache1: #addType text/x-server-parsed-html .shtml # uncomment for Apache2: AddOutputFilter Includes .shtml
chmod a+r,go-rw .htaccess
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int main() {
int cnt=0;
ifstream IN;
IN.open("cnt.txt");
if (IN.is_open()) {
IN >> cnt;
IN.close();
}
cnt++;
ofstream OUT1;
OUT1.open("test.cnt");
OUT1 << cnt << endl;
OUT1.close();
ofstream OUT2;
OUT2.open("test.log", ios::app);
OUT2 << getenv("REMOTE_HOST") << endl;
OUT2.close();
cout << "Content-type: text/plain\n\n" << cnt << endl;
return(0);
}
g++ test.cpp -o test.cgi
chmod a+x,go-rw test.cgi
chmod a+rw-x test.cnt test.cgi
http://your.computer.edu/~your_user_id/cgi-bin/test.cgi
<!--#include virtual="/~your_user_id/cgi-bin/test.cgi" -->in the place where you want the CGI script output to appear.
<!--#exec cgi="/~your_user_id/cgi-bin/test.cgi" -->but the latter is frequently completely disabled.
For increased security move the source code of a CGI script outside of the Web accessible directories. Moving your data files outside these directories might be a good idea too. Directories accessible from the Web are public_html and its subdirectories.