Solved

Improve and debug my visits counter script

Posted on 2011-03-06
7
226 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:patsmitty
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 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

11 Experts available now in Live!

Get 1:1 Help Now