DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Code Snippet: C++ CGI Unique & Raw Hit Counter (http://www.daniweb.com/forums/thread216828.html)

nanodano Nov 18th, 2006 4:35 pm
C++ CGI Unique & Raw Hit Counter
 
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.

  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. }
sneekula Nov 18th, 2006 7:49 pm
Does not look like C code to me! What does it do here?

manutd Nov 18th, 2006 8:47 pm
How about you don't double post code snippets?

~s.o.s~ Nov 19th, 2006 1:38 pm
The duplicate code snippet issue has been dealt with.

manutd Nov 19th, 2006 5:05 pm
void main()Should be int main (void)

~s.o.s~ Nov 20th, 2006 1:12 pm
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 :D

manutd Nov 20th, 2006 4:47 pm
Welcome, my pleasure :)


All times are GMT -4. The time now is 11:32 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC