?
Solved

Improve and debug my visits counter script

Posted on 2011-03-06
7
Medium Priority
?
235 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 2000 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
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
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

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…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…
Suggested Courses

777 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