User Name Password Register
DaniWeb IT Discussion Community
All
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
Nov 18th, 2006
Views: 3,224
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.
cplusplus Syntax
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. bool hasVisited()
  8. // Figure out wether unique hit or not
  9. // Returns true if ip address has visited
  10. {
  11. bool visited = false;
  12. string userIP = getenv("REMOTE_ADDR");
  13. string nonUnique;
  14.  
  15. ifstream input("iplist.cnt");
  16. while (input) { // See if IP is in list
  17. getline(input, nonUnique);
  18. if (userIP == nonUnique) {
  19. visited = true;
  20. break;
  21. }
  22. }
  23. input.close();
  24.  
  25. if (visited == false) { // Add IP to list
  26. ofstream output("iplist.cnt", ofstream::app);
  27. output << userIP << endl;
  28. output.close();
  29. }
  30.  
  31. return (visited);
  32. }
  33.  
  34.  
  35. int main() {
  36. int uniqueHits; // Default in case no counter file exists
  37. int rawHits;
  38.  
  39. ifstream in("counter.cnt");
  40. if (!in) {
  41. uniqueHits = 0;
  42. rawHits = 0;
  43. }
  44. else {
  45. in >> uniqueHits; // 1st number = unique hits
  46. in >> rawHits; // 2nd number = raw hits
  47. in.close();
  48. }
  49.  
  50. bool visited = hasVisited();
  51. rawHits++;
  52. if (!visited)
  53. uniqueHits++;
  54.  
  55. ofstream out;
  56. out.open("counter.cnt");
  57. out << uniqueHits << ' ' << rawHits;
  58. out.close();
  59.  
  60. cout << "Content-type: text/html" << endl << endl
  61. << "<HTML><BODY>" << endl
  62. << "This is a hit counter written in C++. <hr>" << endl
  63. << "Your IP address: " << getenv("REMOTE_ADDR") << ".";
  64. if (visited)
  65. cout << "(Not unique)";
  66. else
  67. cout << "(Unique)";
  68. cout << "<hr>" << endl
  69. << "Unique hits: " << uniqueHits << "<br>Raw hits: " << rawHits << endl;
  70. cout << "</BODY></HTML>" << endl;
  71. }
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
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

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:51 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC