X
X
X
X

Creating Json Visitor Statistics With Php

HomepageArticlesWeb designCreating Json Visitor Statistics Wi...

In this narrative, your visitor information will be stored in a json format file. By not keeping it in the database, we will get rid of an unnecessary pile of information and increase performance a little by not storing it in a txt file. We have written two separate functions for this.

You can add our first function only to the files visited by my visitors or to a file that is called on each page, such as header.


function visitor() {
if(!file_exists('assets/visitor.txt')) {
touch('assets/visitor.json');
}

$array['visitors'][0] = array(
"ip" => realip(),
"date" => date('d.m.Y'),
//"page" => $_SERVER['REQUEST_URI']
);

$read = file_get_contents('assets/visitor.json');
if ($read ==") {
$file = fopen('assets/visitor.json', 'w');
fwrite($file, json_encode($array));
fclose($file);
}else{
$decode = json_decode($read, true);

array_push($decode['visitors'], array(
"ip" => realip(),
"date" => date('d.m.Y'),
//"page" => $_SERVER['REQUEST_URI']
));

$encode = json_encode($decode);

file_put_contents("assets/visitor.json", $encode, LOCK_EX);
}
}
To run the function, just type visitor();.

We need a second function to show the details of the ip and date information that we have stored in the json file in the administration panel.


function visitorgoster($value) {
$json = file_get_contents('assets/visitor.json');
if ($value == 'today') {
$json = json_decode($json,true);
$i = 0;
foreach ($json['visitors'] as $j) {
if (date('d.m.Y',strtotime($j['date'])) == date('d.m.Y')) {
$i++;
}
}
return $i;
}
if ($value == 'unique today') {
$json = json_decode($json,true);
$i = 0;
$tekilyap = array_unique($json);

foreach ($json['visitors'] as $j) {
if (date('d.m.Y',strtotime($j['date'])) == date('d.m.Y')) {
$arr[] = $j['ip'];
}
}
$unique_data = array_unique($arr);
return count($unique_data);
}
if ($value == 'yesterday') {
$json = json_decode($json,true);
$i = 0;
foreach ($json['visitors'] as $j) {
if (date('d.m.Y',strtotime($j['date'])) == date('d.m.Y',strtotime('yesterday'))) {
$i++;
}
}
return $i;
}
if ($value == 'duntekil') {
$json = json_decode($json,true);
foreach ($json['visitors'] as $j) {
if (date('d.m.Y',strtotime($j['date'])) == date('d.m.Y',strtotime('yesterday'))) {
$arr[] = $j['ip'];
}
}
$unique_data = array_unique($arr);
return count($unique_data);
}
if ($value == 'week') {
$json = json_decode($json,true);
$i = 0;
foreach ($json['visitors'] as $j) {
if (date('Y',strtotime($j['date']))) == date('Y') and date('W',strtotime($j['date']))) == date('W')) {
$i++;
}
}
return $i;
}
if ($value == 'haftatekil') {
$json = json_decode($json,true);
$i = 0;
foreach ($json['visitors'] as $j) {
if (date('Y',strtotime($j['date']))) == date('Y') and date('W',strtotime($j['date']))) == date('W')) {
$arr[] = $j['ip'];
}
}
$unique_data = array_unique($arr);
return count($unique_data);
}
if ($value == 'month') {
$json = json_decode($json,true);
$i = 0;
foreach ($json['visitors'] as $j) {
if (date('m.Y',strtotime($j['date'])) == date('m.Y')) {
$i++;
}
}
return $i;
}
if ($value == 'aytekil') {
$json = json_decode($json,true);
$i = 0;
foreach ($json['visitors'] as $j) {
if (date('m.Y',strtotime($j['date'])) == date('m.Y')) {
$arr[] = $j['ip'];
}
}
$unique_data = array_unique($arr);
return count($unique_data);
}
if ($value == 'year') {
$json = json_decode($json,true);
$i = 0;
foreach ($json['visitors'] as $j) {
if(date('Y',strtotime($j['date'])) == date('Y')) {
$i++;
}
}
return $i;
}
if ($value == 'yiltekil') {
$json = json_decode($json,true);
$i = 0;
foreach ($json['visitors'] as $j) {
if(date('Y',strtotime($j['date'])) == date('Y')) {
$arr[] = $j['ip'];
}
}
$unique_data = array_unique($arr);
return count($unique_data);
}
if ($value == 'general') {
$json = json_decode($json,true);
$i = 0;
foreach ($json['visitors'] as $j) {
$i++;
}
return $i;
}
if ($value == 'generic') {
$json = json_decode($json,true);
$i = 0;
foreach ($json['visitors'] as $j) {
$arr[] = $j['ip'];
}
$unique_data = array_unique($arr);
return count($unique_data);
}
}
We have a function where we force visitors to find out the real ip address;


function vercekip() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip=$_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip=$_SERVER['REMOTE_ADDR'];
} return $ip;
}
Display of visitor statistics
Visitcigoster to receive today's visitors('today');

Visitcigoster to receive unique visitors today('unique today');

Visitcigoster to receive yesterday's visitors('yesterday');

Visitcigoster('yesterday') to receive yesterday's unique visitors;

Visitcigoster('week') to receive this week's visitors;

Visitcigoster to receive unique visitors for this week('unique week');

Visitcigoster('month') to receive visitors for this month;

To receive unique visitors for this month, visitcigoster('aytekil');

Visitcigoster to receive visitors for this year('year');

Visitcigoster ('unique year') to receive unique visitors this year;

To get the overall visitor total, click on visitorgoster('general');

To get the total of general unique visitors, use visitorgoster('general singular');


Top