Solved

"visitor counter" cgi script in C, how ?

Posted on 2002-04-03
7
417 Views
Last Modified: 2010-04-15
Hello all,
I need a little C program compiled as .cgi to be called
from an html page. The main objective is to display a
counter for visitors in the web page.
So, I need an example of how this is done from, both,
the C point of view and the HTML point of view.
I need to do this for a Linux web server and a Windows
2000 web server which have the same web page in
two different networks.

Thanks in advance,
Rafael.
0
Comment
Question by:rcr
7 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 6917147
Here ya go - one of a bazillion:
http://www.worldwidemart.com/scripts/C++/textcounter.shtml#Downloading

Just do a search on 'hit counters c source' for more.
0
 

Author Comment

by:rcr
ID: 6919958
Thank you cookre for your answer but ...
the textcounter program is made in C++ and
uses some things which exceed my needs.
I really want a small program in = C = that achieves
what I explained before.
I need a program which does not use something
extra but C and only C and I need the HTML part
to call it, as well.
(I know that you may be thinking ... but why not C++???,
well that is the challenge ... to do it with only C !!! ).

Rafael.
 
0
 
LVL 2

Expert Comment

by:obg
ID: 6950193
Where and how do you want to store your counter? Do you have a database, or do you want to keep it in a file locally? Following very simple solution _should_ work on unix/apache or similar. It's completely untested, and it's been a while since I wrote C-cgis.

#include <stdio.h>

int main()
{
  FILE *f;
  int cnt;

  printf("Content-Type: text/html\r\n\r\n<html><body>\n");
  if ((f = fopen("/tmp/counter", "r")) != NULL) {
    fscanf(f, "%d", &cnt);
    fclose(f);
    if ((f = fopen("/tmp/counter", "w")) != NULL) {
      printf("Hits: %d\n", ++cnt);
      fprintf(f, "%d\n", cnt);
      fclose(f);
    } else
      printf("Can't open file for writing!\n");
  } else
    printf("Can't open file for reading!\n");
  printf("</body></html>\n");
  return 0;
}
0
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
LVL 2

Expert Comment

by:obg
ID: 6950198
Well, that was simple and ugly. I forgot to say that you need /tmp/counter containing 0 (in text), and if your web server runs as nobody, you'll need to set the privileges so that nobody can read and write to this file.
0
 
LVL 2

Expert Comment

by:obg
ID: 6950215
Oops. I wish I could learn to read the full requirements before jumping to conclusions. Are both systems going to use the same counter? You'll need a database if you are going to use this counter from two servers. More info about the database is required before I can tell you how to interact with it.

If both will use the their own local counter, then the above is ok, but for Windoze you might want to choose another filename and path... There is a minimal risk that you miss one count due to simultaneous access, but the risk is so small it almost does not exist. To be sure you should use semaphores.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9478518
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:
PAQ/ No Refund

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Kent (Kdo)
EE Cleanup Volunteer
0
 

Accepted Solution

by:
YensidMod earned 0 total points
ID: 9537470
This question is PAQed and no points refunded (of 100)

YensidMod
Community Support Moderator
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question