<html>
<head>
<title>CGI COUNTER</title>
<meta name="author" content="Kenji INOMOTO (inomoto@in.aitech.ac.jp)">
<meta name="description" content="Gif image access counter by CGI scripts, without using extra routine. Simple and easy to create.">
<meta name="keywords" content="WWW, Internet, Access counter, Gif Image, CGI, Perl, SSI">
</head>

<!-- Revised on 97.6.20/7.15/8.5             Moved to /~inomoto/ on 1997 May 26-->
<!-- Revised on 98.1.12/6.10   -->

<!--#exec cmd="/home/inomoto/public_html/cgi-bin/c_inc_c2.cgi"-->


<body bgcolor="#ccccff" text="#000000" link="#0000ff" alink="#00ff00" vlink="#ff0000">
<body bgcolor="#eeeecc" text="#000000" link="#0000ff" alink="#00ff00" vlink="#ff0000">

<center>
<font FACE="Times New Roman" size=6><b>How to create a simple CGI counter with gif images</b></font>
</center>

<!--#exec cmd="/home/inomoto/public_html/cgi-bin/log_w_c2.cgi"-->

<hr size=2>
<font  face="Times New Roman"color="0000cc" size=4><b>About this counter</b></font><br>
<font size=3>
This is a simple gif counter program written as a perl script.<br>
Only two files are needed. One is a simple counter script. And the other is a set of gif images.<br>
Neither extra graphic routine nor compile procedure is needed.<br>

<hr size=2>
<font  face="Times New Roman"color="0000cc" size=4><b>Requirements</b></font><br>
<font size=3>
<table border=0>
  <tr>
    <td><b>OS</b>;</td><td>Unix</td>
  </tr>
  <tr>
    <td><b>Language</b>;</td><td>Perl</td>
  </tr>
  <tr>
    <td><b>HTTPd</b>;</td><td>NCSA, or Apache</td>
  </tr>
</table>

<hr size=2>
<font FACE="Times New Roman" color="0000cc" size=4><b>Summary</b></font><br>
Follow the five steps below to create this counter.<br>
<center>
<table border=1>
<tr>
   <th>Step</th>
   <th>File name</th>
   <th>Creation</th>
   <th>File type</th>
   <th>Comments</th>
</tr>
<tr>
   <td><b>1.</b> usage of ssi and cgi</td>
   <td><center>.htaccess</center></td>
   <td><center>new</center></td>
   <td><center>text</center></td>
   <td><center>step <b>1</b></center></td>
</tr>
<tr>
   <td><b>2.</b> ssi scripts</td>
   <td><center>(in your html document)</center></td>
   <td><center>insertion</center></td>
   <td><center>text</center></td>
   <td><center>step <b>2</b></center></td>
</tr>
<tr>
   <td><b>3.</b> cgi scripts</td>
   <td><center>count.cgi</center></td>
   <td><center>new</center></td>
   <td><center>text</center></td>
   <td><center>step <b>3</b> and [6]</center></td>
</tr>
<tr>
   <td><b>4.</b> count data file</td>
   <td><center>count.txt</center></td>
   <td><center>new</center></td>
   <td><center>text</center></td>
   <td><center>step <b>4</b></center></td>
</tr>
<tr>
   <td><b>5.</b> gif images</td>
   <td><center>0.gif...9.gif</center></td>
   <td><center>new</center></td>
   <td><center>gif</center></td>
   <td><center>download</center></td>
</tr>
</table>
</center>
<br>

<hr size=2>
<br>

<font FACE="Times New Roman" color="0000cc" size=4><b>Step 1.</b></font><font size=4 color="000000"> Allow usage of cgi scripts in your directory.</font><br>
<br>
1.1 Make a text file named .htaccess and copy the next two text lines into it.<br>
<font size=3>
<pre>
          AddType text/x-server-parsed-html .html
          AddType application/x-httpd-cgi   .cgi
</pre></font>
CAUTION; If your home page has a file name extension different from ".html", change the ".html" in the upper line to match your extension. <br>
1.2 Put this file in the same directory as your home page on the Web server.<br>
<br>
<hr size=2>
<br>

<font FACE="Times New Roman" color="0000cc" size=4><b>Step 2.</b></font><font size=4 color="000000">Call cgi script from your html documents.</font><br>
<br>
2.1 Insert the next script into the html document which is the base of your home page.
<pre><font size=3>
          &lt!--#exec cmd="cgi-bin/count.cgi"--&gt
</font></pre>
<hr size=2>
<br>

<font FACE="Times New Roman" color="0000cc" size=4><b>Step 3.</b></font><font size=4 color="000000"> Set perl script of </font><font size=4>[6]</font><font size=4> in your directory as count.cgi.</font><br>
<br>
3.1 Prepare "count.cgi" as shown in [6].<br>
3.2 Put cgi script on the Web server.<br>
  (1) Change directory to where you want to install this counter.<br>
  (2) Put "count.cgi" in the directory.<br>
  (3) Do chmod 755 for "count.cgi".<br>
  (4) If you create new directory for "count.cgi", do chmod 777 for it.<br>
<br>
<hr size=2>
<br>

<font FACE="Times New Roman" color="0000cc" size=4><b>Step 4.</b></font><font size=4 color="000000"> Create counter file and chmod.</font><br>
<br>
4.1 Change directory to your home page and execute following commands.
<font size=3>
<pre>
          echo 0 > count.txt
          chmod 766 count.txt
</pre></font>
CAUTION;The full path name to this file is used by $count in [6]. 
<br>
<hr size=2>
<br>

<font FACE="Times New Roman" color="0000cc" size=4><b>Step 5.</b></font><font size=4 color="000000"> Prepare gif images.</font><br>
<br>
5.1 Download suitable counter images from <a href="http://www.digitmania.holowww.com/">http://www.digitmania.holowww.com/</a>, or from other sites, and unzip them.<br>
5.2 Rename these images to "0.gif"..."9.gif".<br>
5.3 Put gif images on the Web server.<br>
(1) Put them in the directory indicated by $image.<br>
(2) Do chmod 744 *.gif.<br> 
(3) If you create a new directory for these images, do chmod 777 for it.<br>
<br>
Now, the installation procedure is completed.<br>
<br>
<hr size=2>
<br>

<font FACE="Times New Roman" color="0000cc" size=4><b>[6] count.cgi</b></font><br>
<br>
  (1) Select the scripts between the two dotted lines shown below.<br>
  (2) COPY them to your text editor, for example, notepad or something else.<br>
  CAUTION; Do not use "Save As..." in "File" command of your browser, because "<" and ">" are escaped to "lt" and "gt".<br>
  (3) Determine the two variables as below;<br>
         $image ; indicate your directory name where the gif images are located. (cf. 5.3)<br>
         $count ; indicate the path name to the access number file. (cf. 4.1)<br>
  (4) SAVE this file as "count.cgi"<br>

<font size=3>
<pre>
-------------------------------------------------------------------------------
#!/usr/local/bin/perl

# Gif images should be named as 0.gif, 1.gif, ..., 9.gif
#
# Permission is granted to copy and redistribute this program if this notice
# remains intact.
#
# Written by    inomoto@in.aitech.ac.jp      12 Feb. 1997
# based on the "Matt's cgi counter" at http://www.worldwidemart.com/
#
# If you find "&" preceded "lt" or "gt",
#    replace these three letters by "<" or ">" respectively.

   $| = 1;
   $image = "directory of gif images";
   $count = "directory and file name of count data";

# Get counter number and increment it
   open(COUNT,"$count") || die "Can't open count data file for Reading: $!\n";
   $c = &ltCOUNT&gt ;
   close(COUNT);
   if ($count =~ /\n$/) {
      chop($c);
   }
   $c++;
   open(COUNT,">$count") || die "Can't open count data file for Writing: $!\n";
   print COUNT "$c";
   close(COUNT);

# Determine counter width
   $num = $length = length($c);

# Set Individual Counter Numbers Into Associative Array
   while ($num > 0) {
      $CHAR{$num} = chop($c);
      $num--;
   }

# Output gif images to Web server
   $j = 1;
   print "&ltimg src=$image/0.gif&gt";
   print "&ltimg src=$image/0.gif&gt";
   while ($j <= $length) {
      print "&ltimg src=$image/$CHAR{$j}\.gif&gt";
      $j++;
   }
-------------------------------------------------------------------------------
</pre></font>
<hr size=2>
</font>

<a href="counter3.htm">Additional Notes</a>    <a href="howtocgi.htm">Japanese version</a><br>
<a href="mailto:inomoto@aitech.ac.jp">Email</a><br>
<!--a href="../ino/index_e.htm">Home page</a><br--> 
<br><br><br>
</body>
</html>






