2

i want to disply total no of visitors on my php website , i found some html / php scripts that show a counter like this

<!-- Start of SimpleHitCounter Code -->
<div align="center"><a href="http://www.simplehitcounter.com" target="_blank"><img src="http://simplehitcounter.com/hit.php?uid= " border="0" height="18" width="83" alt="web counter"></a><br><a href="http://www.simplehitcounter.com" target="_blank" style="text-decoration:none;">web counter</a></div>
<!-- End of SimpleHitCounter Code -->

i tried it in localhost and issue is that if i refresh same page it goes on adding to count how do i get unique count for each visitor please help me , i could not find any proper source while searching

3
  • Use Google Analytics if you don't want to re-invent the wheel. Nov 14, 2013 at 13:12
  • 1
    check with the IP that you are opening a page and avail the increment of the hit counter for one time for a unique IP
    – GautamD31
    Nov 14, 2013 at 13:12
  • that will show vistor count in my hope page ?
    – user2088260
    Nov 14, 2013 at 13:12

4 Answers 4

0

You can have a database table for it, column values should be id, total_count and ip address. Hence if you find same ip address again do not increase the count it is increased.

If you want to increase count once in a day from one system, you can add one more column called last_hit_date. Use this to verify if the ip already clicked for the day or not.

0

check with the IP that you are opening a page and avail the increment of the hit counter for one time for a unique IP .

Means first time if you open a page with your IP if it is unique in the hit counter Db then nincrement the hit counter and also store your IP into DB.Then for each page refresh you will check for the with the current IP is existed or not.If not then increment and if it has neglect it.

0

It depends what counts as unique visitor. One solution is to check IP address, but you might have multiple people using different computers behind a wifi router for example, and if you want all of them to count as unique it would be better to use cookies.

Another advantage of cookies is that you can track repeat visitors easily without having to store a database of IP addresses on your server.

2
  • Would upvote for cookies, but there's no point of not having a db.
    – user1537415
    Nov 14, 2013 at 13:23
  • @Christian. One thing is having a db with a simple one-row counter (just increase whenever cookie is not found) versus having a db of ALL IP addresses that access the site. Well, it depends on the amount of traffic, but still. Nov 14, 2013 at 13:47
0

Create a database with the table ips, and add columns id,ip,lasthit to it. Snippet below will add a hit to the database if an user visits the site (one hit/date)

<?php
$pdo = new PDO('mysql:host=localhost;dbname=myDatabase','username','password');    $time = date("Y-m-d");
$query = "INSERT INTO ips (ip, lasthit) VALUES (?,?) WHERE lasthit <> ?";
$smt = $pdo->prepare($query);
$smt->execute(array($_SERVER['REMOTE_ADDR'],$time, $time));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.