C++ CGI Unique & Raw Hit Counter

nanodano nanodano is offline Offline Nov 18th, 2006, 4:35 pm |
0
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.
Quick reply to this message  
C++ 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. }
0
sneekula sneekula is offline Offline | Nov 18th, 2006
Does not look like C code to me! What does it do here?
 
0
manutd manutd is offline Offline | Nov 18th, 2006
How about you don't double post code snippets?
 
0
~s.o.s~ ~s.o.s~ is offline Offline | Nov 19th, 2006
The duplicate code snippet issue has been dealt with.
 
0
manutd manutd is offline Offline | Nov 19th, 2006
void main()Should be int main (void)
 
0
~s.o.s~ ~s.o.s~ is offline Offline | 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
 
0
manutd manutd is offline Offline | Nov 20th, 2006
Welcome, my pleasure
 
 

Message:


Similar Threads
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC