•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,507 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,821 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
This code snippet actually contains a few nifty things. There are two functions, hasVisited() and main(). hasVisited() makes use of a very handy function for CGI programmers: getenv. You can see I use it to retrieve REMOTE_ADDR which is the person's IP address. In main() it gets the count from a file and increments if needed and outputs back to the same file. It then outputs a simple HTML page that the user sees.
Last edited : Nov 20th, 2006.
#include <iostream> #include <fstream> #include <string> using namespace std; bool hasVisited() // Figure out wether unique hit or not // Returns true if ip address has visited { bool visited = false; string userIP = getenv("REMOTE_ADDR"); string nonUnique; ifstream input("iplist.cnt"); while (input) { // See if IP is in list getline(input, nonUnique); if (userIP == nonUnique) { visited = true; break; } } input.close(); if (visited == false) { // Add IP to list ofstream output("iplist.cnt", ofstream::app); output << userIP << endl; output.close(); } return (visited); } int main() { int uniqueHits; // Default in case no counter file exists int rawHits; ifstream in("counter.cnt"); if (!in) { uniqueHits = 0; rawHits = 0; } else { in >> uniqueHits; // 1st number = unique hits in >> rawHits; // 2nd number = raw hits in.close(); } bool visited = hasVisited(); rawHits++; if (!visited) uniqueHits++; ofstream out; out.open("counter.cnt"); out << uniqueHits << ' ' << rawHits; out.close(); cout << "Content-type: text/html" << endl << endl << "<HTML><BODY>" << endl << "This is a hit counter written in C++. <hr>" << endl << "Your IP address: " << getenv("REMOTE_ADDR") << "."; if (visited) cout << "(Not unique)"; else cout << "(Unique)"; cout << "<hr>" << endl << "Unique hits: " << uniqueHits << "<br>Raw hits: " << rawHits << endl; cout << "</BODY></HTML>" << endl; }
Comments (Newest First)
manutd | Junior Poster in Training | Nov 20th, 2006
•
•
•
•
Welcome, my pleasure
~s.o.s~ | Rebellion Revamped | Nov 20th, 2006
•
•
•
•
Has been taken care of, snippet modified to incorporate the change.
Thanks for notifying this, your help in the code snippet section is greatly appreciated.
Hope you find out more such bugs
Thanks for notifying this, your help in the code snippet section is greatly appreciated.
Hope you find out more such bugs
manutd | Junior Poster in Training | Nov 19th, 2006
•
•
•
•
void main()Should be int main (void) ~s.o.s~ | Rebellion Revamped | Nov 19th, 2006
•
•
•
•
The duplicate code snippet issue has been dealt with.
manutd | Junior Poster in Training | Nov 18th, 2006
•
•
•
•
How about you don't double post code snippets?
sneekula | Nearly a Posting Virtuoso | Nov 18th, 2006
•
•
•
•
Does not look like C code to me! What does it do here?
Post Comment
•
•
•
•
DaniWeb Marketplace (Sponsored Links)