|
|
Programming a Web Counter in CUsing a Common Gateway Interface to run native executables on the web host, using the Server Side Include (SSI) function.Programming project in C - a simple web counter. Requires some knowledge of HTML, some web space and a C compiler. Linux recommended : have fun!
Programming Project in CSimple Web Counter IntroductionFor those who have access to the cgi/bin directory of their web host, a simple web counter is a simple project to complete in an afternoon. It is also a great starting point for learning about input, output, and Common Gateway Interface (CGI) programming. The key to this project lies in being able to run native executables (programs) on the web host, using the Server Side Include (SSI) function of the web server to include in-line results of that application. Put more simply, in serving a web page, certain instructions can cause the server to perform additional commands in order to supply the correct information to the web browser. SSI causes output to be included within the web page, as it is served; all the browser receives is the output of the program, within the web page. The HTMLIn order to execute the application, you will need to insert an SSI directive into the web page. This we shall cover later on, but if you really cannot wait, you can find out all about SSI from your system documentation, or the Internet. Some clues : you either ned to execute the application directly, after compiling it for your platform or use a script (in Perl, for example) to run the application for you. The CounterA simple web counter just keeps track of the number of visitors - adding one for each time that the page is loaded. A text file stores the current number, and output is in plain text; although this can be enriched with HTML. The program has three parts: reading in the current number from the data file, adding one to it, and writing out the new number, and then writing that number to the data file. One thing to remember is that any CGI program needs to output is the type of content to be rendered (text, image, etc.), followed by the items to be rendered, so that the server can communicate correctly with the client. The CodeThe full application code can be downloaded here, but some of the pertinent snippets follow. All output is directed to standard out (or stdout), so we can just use the usual printf statements to direct information to the server, starting with: printf("Content-type: text/html\n\n"); The instruction for reading the data from the file is reasonably simple: fread ( &ldPageHits, sizeof(&ldPageHits), 1, hFile ); In this snippet, it is worth pointing out that we pass a reference to ldPageHits, so that the fread function (from stdio.h) can update it. If we pass by value, it becomes inaccessible to the function called. We can then print out the counter, in plain text: printf ( "%015ld", ldPageHits ); We can pass by value, since ldPageHits is a read-only parameter in this case. We have also indicated that the value should be formatted to 15 characters wide, preceded by zeros. To write out the new value of ldPageHits, we simply use: fwrite ( &ldPageHits, sizeof(ldPageHits), 1, hFile ); We are passing by reference here, because the first parameter can accept a pointer to any kind of data, and is treated as binary memory data. If this were not the case, then we would need several functions (one for each data type) which would be difficult to maintain. If you look up the function prototype for fwrite, you will note that the first parameter is a void * pointer, which allows the parameter to take a variety of pointer values, and be cast into a data type suitable for writing directly to a file. Next StepsKeeping a log file of page hits would enable a better idea of page load activity to be formed, and identifying the pages that are being loaded would enrich this further. Beyond this, there are also some ways that information provided by the browser to the server can be retrieved so that the referring page (last page visited) can be tracked - usually used for tracking search engine referrals, and visitor paths. However, these are better managed using a PHP version of the counter, which we will look at in a separate article.
The copyright of the article Programming a Web Counter in C in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Programming a Web Counter in C in print or online must be granted by the author in writing.
|
|
|
|
|
|
|
|