confused and dazed Posted April 16, 2012 Share Posted April 16, 2012 Hello internet. I would like to get a hit counter on my website but the only counters I have been able to find across the internet make me accesses someone else's site. I would like to contain the counter in my own site with my own code. Recommendations are appreciated. Thanks! Link to comment Share on other sites More sharing options...
thescientist Posted April 16, 2012 Share Posted April 16, 2012 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 More sharing options...
confused and dazed Posted March 1, 2013 Author Share Posted March 1, 2013 (edited) 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 March 1, 2013 by confused and dazed Link to comment Share on other sites More sharing options...
justsomeguy Posted March 1, 2013 Share Posted March 1, 2013 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 More sharing options...
confused and dazed Posted March 1, 2013 Author Share Posted March 1, 2013 justsomeguy - thanks for responding.I realized that right after I hit POST... I am going to use mysql to pull the latest number and increment it that way.I meant IP tracking for unique IP addresses. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 1, 2013 Share Posted March 1, 2013 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 More sharing options...
confused and dazed Posted March 3, 2013 Author Share Posted March 3, 2013 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now