Jump to content

Hit counters - would like recommendations


confused and dazed
 Share

Recommended Posts

The simplest implementation I can think of would just to have an AJAX function execute on page load to a server side script (like PHP) that can do one of two things. Read in a file and increment the value of the number in it. Or use a database, and make pages table, and increment the value in the table. additionaly, you would want to save the page for the hit, so would want to pass a referrer value from the JS function to the script. Personally I would recommend using a database for this and doing the incrementing/UPDATE with SQL. Also, you may to use some sort of IP tracking to make sure hits are unique and daily.

Link to comment
Share on other sites

  • 10 months later...

Hello Internet.I am finally getting back around to this topic. I know this is about as simple as you can get but the code below works.I would like to be able to do a unique IP mapping but I don't know how this would work with the code I selected. Any ideas?I used the following code //used prior to <html> tag<?phpsession_start();if(isset($_SESSION['views']))$_SESSION['views']=$_SESSION['views']+1;else$_SESSION['views']=1;?> <html><head></head> <body>//used after <html> tag and within the <body> tag<?phpecho "Page Views ". $_SESSION['views'];?></body></html>

Edited by confused and dazed
Link to comment
Share on other sites

What do you mean by an IP mapping? You can get the user's IP from $_SERVER['REMOTE_ADDR']. Note that using the session like you're doing is only counting the page views for that user, and it's not being saved anywhere. Each user has their own counter, and it resets when their session expires.

Link to comment
Share on other sites

If you want to count IP addresses then you can store those in the database instead of a counter. The counter will be the number of IP addresses. You can make the IP a unique key in the table and when someone comes to your site you can use INSERT IGNORE to add the IP to the table if it doesn't exist.

Link to comment
Share on other sites

justsomeguy - what I ended up doing is creating a column in my table called IP_ad and made it a UNIQUE index. Then I updated my code to run a query on IP_ad to SELECT DISTINCT and count the rows. Then I sent that number to the database using INSERT INTO a column called count_IP. Finally I pull that number using window.onload and wala... my very own site counter!!! Thanks for the advice!!!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...