?
Solved

"visitor counter" cgi script in C, how ?

Posted on 2002-04-03
7
Medium Priority
?
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 46

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

762 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