Solved

"visitor counter" cgi script in C, how ?

Posted on 2002-04-03
7
409 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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:Kdo
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

919 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now