Solved

"visitor counter" cgi script in C, how ?

Posted on 2002-04-03
7
405 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:obg
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
This question is PAQed and no points refunded (of 100)

YensidMod
Community Support Moderator
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

763 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

13 Experts available now in Live!

Get 1:1 Help Now