Solved

"visitor counter" cgi script in C, how ?

Posted on 2002-04-03
7
412 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to show menu 5 88
How to learn Linux? 10 60
Why isn't object file created? 6 57
Constant string is of type char *   ? 7 28
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…
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…
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.

813 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

16 Experts available now in Live!

Get 1:1 Help Now