Solved

Improve and debug my visits counter script

Posted on 2011-03-06
7
228 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 dynamically set the form action using jQuery.

786 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