Programming Great Village

Create a good visitor-counter with php

Create a good visitor-counter with php

You can get some free visitor-counter on internet but this issue may have two problems:

- You cannot control them as well.

- Possibly, some features is not free.

But you can create a free good visitor-counter with php, if you know a little about php.

You can use ‘ip address’ for creating the above system. It means that every IP can register once a day. Its example is here:

Create a good visitor-counter with php

This is the visitor-counter system of ‘Programming Great Village’ site. In this system we calculate the number of visitors by their IPs. In this system, the number of visitors is not unreal and repetitious.

This is all of my codes for creating this system:


1     $pr = true;

2     date_default_timezone_set('America/New_York');

3     if (isset($_SERVER['REMOTE_ADDR']))

4     $ip = $_SERVER['REMOTE_ADDR'];

5     else

6     $ip = "none";

7     if (file_exists("data/visitors.bn")) {

8     $rrr = fopen("data/visitors.bn", "r");

9     while (($line = fgets($rrr)) != false) {

10     $cc = explode("|", $line);

11     if (date("M/d") == $cc[0] && $ip === $cc[1]) {

12     $pr = false;

13     break;

14     }

15     }

16     fclose($rrr);

17     }

18     if ($pr) {

19     $rfi = fopen("data/visitors.bn", "a");

20     fwrite($rfi, date("M/d")."|$ip|".date("H:i")."|"."\r\n");

21     fclose($rfi);

22     }

It’s better try it in real server. Because if you try it in local server, this counter just counts once a day.

I think that these codes are simple.so I just sum up it.

First we check if the file (visitors.bn) exists and then check if the user IP exists in the file and finally in case that there is no user IP, write it in the file.

And then in another function, open the file and do as follows:


1     if (file_exists("data/visitors.bn")) {

2     $rfi = fopen("data/ visitors.bn", "r");

3     $md = date("M/d");

4     $count = 0;

5     $count1 = 0;

6     $count2 = 0;

7     while (($line = fgets($rfi)) != false) {

8     if (empty($line))

9     continue;

10     $count1++;

11     $cc = explode("|", $line);

12     if ($md == $cc[0])

13     $count++;

14     //*****

15     $ye1 = explode("/", $cc[0]);

16     $ye2 = explode("/", $md);

17     if ($ye1[0] == $ye2[0] && ($ye1[1] + 1) == $ye2[1])

18     $count2++;

19     

20     }

21     fclose($rfi);

22     

23     echo "<p><b>›</b> <b>Visitors Today(Unique IP)</b> : $count</p>";

24     echo "<p><b>›</b> <b>Visitors Yesterday(Unique IP)</b> : $count2</p>";

25     echo "<p><b>›</b> <b>Total Visitors(Unique IP)</b> : $count1</p>";

26     }

Keywords :phpvisitor-counterip address
Written by : amindelavar  |  Posted on : 2017/01/01-10:38   |  Number of Visitors :6813
Post a comment  (You must first register on the site to be able to comment.)

Is this article useful ?    Yes    No

If you have more time, you can make your opinion more fully described below :

Search Box
Recent Posts
Most Viewed Posts
Site Statistics

 All articles available : 16

 The number of Registered Users : 3

 Last Updated site : 6 month(s) ago

 Visitors Today(Unique IP) : 8

 Visitors Yesterday(Unique IP) : 7

 Total Visitors(Unique IP) : 3686