Solved

Improve and debug my visits counter script

Posted on 2011-03-06
7
227 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now