Running your own CGI scripts: script that writes to a file

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.

  1. Login to your account.
  2. Type:
  3. 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
  4. Create and edit the text file that overrides the local configuration for the Web server: .htaccess
  5. 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
  6. Type:
  7. chmod a+r,go-rw .htaccess
  8. Create and edit a text file with the CGI script source code: test.cpp
  9. #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);
    }
  10. Compile that file. Type:
    g++ test.cpp -o test.cgi
  11. If there is no errors make it executable by anyone. You need to repeat this step after each compilation. Type:
    chmod a+x,go-rw test.cgi
  12. Create two files: test.cnt and test.log. They can remain empty but must be created
  13. Enable these two files for writing by anyone (and also by your CGI script). Type:
    chmod a+rw-x test.cnt test.cgi
  14. Run a Web browser. Open the following location:
    http://your.computer.edu/~your_user_id/cgi-bin/test.cgi
  15. Reload the page a few times from different computers. Check the content of the test.cnt and test.log files.
  16. Now you can place the link to your counter inside a regular HTML file. In order to do that
    1. Change the file extension to .shtml and update all links to it in other files accordingly.
    2. Place
      <!--#include virtual="/~your_user_id/cgi-bin/test.cgi" -->
      in the place where you want the CGI script output to appear.
      Alternatively you may use
      <!--#exec cgi="/~your_user_id/cgi-bin/test.cgi" -->
      but the latter is frequently completely disabled.
    3. You need to use a plain text editor to include SSI features in your Web pages. Do not use Netscape or other HTML editor.
  17. Good luck! You made it!

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.

web site front local main page print this page   general bookmarks software bookmarks go back close this page copyright info