Solved

Improve and debug my visits counter script

Posted on 2011-03-06
7
230 Views
Last Modified: 2012-05-11
Hi

I have written a script that keeps track of how many visits I receive, not necessarily unique, and refreshing the page twice results in two hits, that is fine by me..

I work from two different places but also failed to correctly add two restricted ip addresses so it now counts my home's ip, but not that other, while it should not count any of the two.

But sometimes it resets the value to zero and then I have lost the real count and have to gamble what it was. I know how it resets itself, but I do not know why it does it. And also, I think it could be written much shorter and more efficient, so if anyone would want to help me that I would very much appreciate it
<?php
$myip = "82.171.85.188";
if(!strstr($myip, $_SERVER['REMOTE_ADDR']) ){
	
 $indexHits = realpath("incl/index.php.dat");
  $indexHits2 = trim(file_get_contents($indexHits));
  if ($indexHits2 != ""){ ++$indexHits2;}
  else { $indexHits2 = 1;}
  $fp = fopen($indexHits,"w");
  fputs($fp,$indexHits2);
  fclose($fp);
  printf("%05d", $indexHits2);
}
else 
{
 $indexHits = realpath("incl/index.php.dat");
  $indexHits2 = trim(file_get_contents($indexHits));
 $fp = fopen($indexHits,"r");
   fclose($fp);
  printf("%05d", $indexHits2);
}

?>

Open in new window

0
Comment
Question by:PeterdeB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 3

Accepted Solution

by:
wwwdeveloper2 earned 500 total points
ID: 35046818
Do you have the option of using a database table or Google Analytics?  While I think it is pretty neat that you are trying to do it with a .dat file, I think that there are so many things that could go wrong with this model.

What would happen if you get more that one person visiting your site at the same time? Would your counts be offset?

If it was me and Google Analytics wasn't an option, I think I would create a couple of db tables.  1 to store all of the restricted ip addresses, and another to keep my count.

I would first grab the IP like you are doing and do a quick lookup to see if it is restricted, if there is a match don't do anything.  If there isn't a match, jump into your loop to grab the current count from the db and increment by 1 then update that row.

Storing your restricted IPs in the db, would allow for more flexibility with adding new IPs without having to touch your code.

Just some initial thoughts and something to think about if you are planning to roll this code out into a production environment with real[world traffic hitting your site.

Google Analytics really has some awesome data about your visitors, and it is also fun to see the new upgrades they roll out on a regular basis.
0
 

Author Comment

by:PeterdeB
ID: 35047300
Ok, good thinking I will try to use a database then I think but will let you know of course if it works out and so on, thanks for your fast reply
0
 
LVL 11

Expert Comment

by:Bruce Smith
ID: 35047303
Woopra is also a very good one. They have a free-version that is great for websites that get moderate amount of visits.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:wwwdeveloper2
ID: 35048354
PeterdeB - No problem.  Be sure to hit me up with any other questions you might have.  If you are satisfied with the answer, will you please close the question and award points?

Thanks!
0
 

Author Comment

by:PeterdeB
ID: 35068972
Ofcourse I will finalize this question, I have made the decision to not use my own script indeed wwwdeveloper2, have tested it, and well i do not get many visitors, but it currently is at 45000 so every day 30 more, will not make it much bigger that textfile, but those 45000 will take time to load
0
 

Author Comment

by:PeterdeB
ID: 35068974
patsmity thanks for your reply as well
0
 

Author Closing Comment

by:PeterdeB
ID: 35068982
Thanks I guess I just was kind of proud of my script but it is a no go
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question