Itz Raghu
Itz Raghu

Reputation: 457

adding counter to php page to count the unique visitors

I want to add a counter in my webpage which counts the number of visitors. But my problem is that when i refresh my page ,counter increases by 1..i want that counter increases only when a new visitor with another ip reaches to my webpage. here are my codes.. Sorry for my weak english

index.php

   <?php
session_start();

$ip = $_SERVER['REMOTE_ADDR']; 
$_SESSION['current_user'] = $ip;


if(isset($_SESSION['current_user']))
{
    $count = file_get_contents("counter.txt");
    $count = trim($count);
    $fl = fopen("counter.txt","w+");
    fwrite($fl,$count);
    fclose($fl);


}

else
{
    $count = file_get_contents("counter.txt");
    $count = trim($count);
    $count = $count + 1;
    $fl = fopen("counter.txt","w+");
    fwrite($fl,$count);
    fclose($fl);

}

Upvotes: 0

Views: 16584

Answers (4)

Best And Easy Code

Try to store the user IP in database and check for unique user

 $`servername` = "";
 $username = "";
 $password = "";
 $`dbname` = "";
 $`conn` = new `mysqli`($`servername`, $username, $password, $`dbname`);

 if ($`conn`->connect_error) {
     die("Connection failed: " . $`conn`->connect_error);
 } 
 $address = gethostbyaddr($_SERVER['REMOTE_ADDR']);
 $name = `gethostname`();

 $re = "select * from visitor where name='$name'";
 $call = `mysqli_fetch_array`($re);
 $as = `mysqli_num_rows`($call);
 if($as == 0){
    $`sql` = "UPDATE visitor SET visits = visits+1 WHERE name = '$name'";
 }else{
 $`sql` = "INSERT INTO visitor(visits,name,address) VALUE(1,'$name','$address')";
 }
 $`conn`->query($`sql`);
 $`sql` = "SELECT visits FROM visitor WHERE id = 1";
 $result = $`conn`->query($`sql`);
 if ($result->`num_rows` > 0) {
     while($row = $result->fetch_assoc()) {
         $visits = $row["visits"];
     }
 } else {
    $visits = "";
     //echo $visits;
 }
  `$conn`->close();

Upvotes: 0

Beroza Paul
Beroza Paul

Reputation: 2117

As database based solution is not preferred, You can try the following file based solution for counting unique visitor. You already have used counter.txt file in your code.

I tried to use the same file that you have used. In my case I am storing IP address in that file. I have used base64 encoding function just to hide the IP address. It is always good to keep that file in a safe place. If that file is lost then the unique visitor IPs will be lost. See the function below:

Function definition

function getUniqueVisitorCount($ip)
{
    session_start();
    if(!isset($_SESSION['current_user']))
    {
        $file = 'counter.txt';
        if(!$data = @file_get_contents($file))
        {
            file_put_contents($file, base64_encode($ip));
            $_SESSION['visitor_count'] = 1;
        }
        else{
            $decodedData = base64_decode($data);
            $ipList      = explode(';', $decodedData);

            if(!in_array($ip, $ipList)){
              array_push($ipList, $ip);
              file_put_contents($file, base64_encode(implode(';', $ipList)));
            }
            $_SESSION['visitor_count'] = count($ipList);
        }
        $_SESSION['current_user'] = $ip;
    }
}


Function call

$ip = '192.168.1.210'; // $_SERVER['REMOTE_ADDR'];
getUniqueVisitorCount($ip);
echo 'Unique visitor count: ' . $_SESSION['visitor_count'];


Output

Unique visitor count: 2

Upvotes: 2

user4628565
user4628565

Reputation:

try to store the user IP in database and check for unique user,

<?php 
session_start();
if (!$_SESSION['status']) {
$connection = mysql_connect("localhost", "user", "password");
mysql_select_db("ip_log", $connection);

$ip = $_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO `database`.`table` (IP) VALUES ('$ip')");

mysql_close($connection);
$_SESSION['status'] = true;
}

?>

Upvotes: 0

MaggsWeb
MaggsWeb

Reputation: 3027

Change:

if(isset($_SESSION['current_user']))

to:

if($_SERVER['REMOTE_ADDR'] == $_SESSION['current_user'])

And, surely you dont need to get $count from a file, and then write the same value back to the file...? If the $_SERVER['REMOTE_ADDR'] matches the SESSION['current_user'] then do nothing..

Upvotes: 0

Related Questions