Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Visit stats plugin v1

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
Aliresalat
Smarty Rookie


Joined: 09 Feb 2014
Posts: 23
Location: Iran

PostPosted: Thu Jul 24, 2014 6:38 am    Post subject: Visit stats plugin v1 Reply with quote

View statistics without using a database with PHP

Code:


<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * visit_stats plugin v1
 * File name: function.visit_stats.php
 * Name:     visit stats
 * Date:     2014.07.23
 *
 * Examples: {visit_stats}
 *
 * @author : Ali Resalat (300) <info.Aliresalat@Gmail.com>
 * @Yahoo ID : Mitooo.Mitooo
 */
 
 
function smarty_function_visit_stats($params, &$smarty)
{

//Time difference between server
$time_zone = '12600';
//Today
$today = date("Y-m-d", time()+$time_zone);
//Yesterday
$yesterday = date("Y-m-d", time()-86400+$time_zone);
//File Address
$file_src = 'visit-stats.txt';
chmod($file_src, 0755);
//Reading File
$read_file = file_get_contents($file_src);
//If the file is not empty
if (filesize($file_src) > 0 || $read_file != ''){
    $split_file = explode('|', $read_file);
    //print_r($split_file);
    $modify = $split_file[3];
    //If today's date is not equal to Last
    if($modify != $today){
        $today_visit = 1;
        $yesterday_visit = $split_file[0];
        $total_visit = $split_file[2] + 1;
        $last_modify = $today;
    }
    //If on the date it was last modified
    else{
        $today_visit = $split_file[0] + 1;
        $yesterday_visit = $split_file[1];
        $total_visit = $split_file[2] + 1;
        $last_modify = $today;
    }
}
//If the file was empty
else{
    $today_visit = 1;
    $yesterday_visit = 0;
    $total_visit = 1;
    $last_modify = $today;
}
//Writing new data in the file
$file_src_handle = fopen($file_src, 'w+');
$visit_data = $today_visit.'|'.$yesterday_visit.'|'.$total_visit.'|'.$last_modify;
fwrite($file_src_handle, $visit_data);
fclose($file_src_handle);
//Calculate the number of online users
$config_array = array(
'user_time' => date("YmdHis", time()+$time_zone),
'user_ip' => $_SERVER['REMOTE_ADDR'],
'file_name' => 'visit-online.txt'
);
chmod($config_array['file_name'], 0755);
//Reading the file
$online_file = file_get_contents($config_array['file_name']);
//Array analysis
$online_file = explode("\r\n", $online_file);
//Remove empty values
foreach($online_file as $key=> $value){
    if(is_null($value) || $value == ''){
        unset($online_file[$key]);
    }
}
//Remove the old IP and current IP
foreach($online_file as $key=> $value){
    $user_ip_time = explode("|", $value);
    if($user_ip_time[1] <= date("YmdHis", time()+$time_zone - 300)){
        unset($online_file[$key]);
    }
    if($user_ip_time[0] == $config_array['user_ip']){
        unset($online_file[$key]);
    }
}
//Calculate Number Online
$online = 1;
foreach($online_file as $online_users){
    $user_ip_time = explode("|", $online_users);
    if($user_ip_time[1] >= date("YmdHis", time()+$time_zone - 300)){
        $online++;
    }
}
//Stats online users are added to the current user
$new_online = $config_array['user_ip'] . "|" . $config_array['user_time'] . "\r\n";;
foreach($online_file as $key=> $value){
    $new_online .= $value . "\r\n";
}
//Writing new data in the file
$file_src_handle = fopen($config_array['file_name'], 'w+');
fwrite($file_src_handle, $new_online);
fclose($file_src_handle);
////////////////* http://www.Aliresalat.ir *///////////////
//Get Out
echo "

<style>
body{
    font-family:Tahoma, Geneva, sans-serif;
    font-size:12px;
}
.stats{
    display:block;
    margin-left:auto;
    margin-right:auto;
    width:150px;
    height:auto;
    border:1px solid #CCC;
    padding:4px;
    line-height:20px;
}
</style>

<div class=\"stats\">
&raquo; Today Visit : $today_visit <br />
&raquo; Yesterday visit : $yesterday_visit <br />
&raquo; Online user: $online <br />
&raquo; Total visit: $total_visit
</div>";

}

/* vim: set expandtab: */

?>



Examples: {visit_stats}

Output:
Today Visit : 1
Yesterday visit : 15
Online user: 1
Total visit: 16
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger
Aliresalat
Smarty Rookie


Joined: 09 Feb 2014
Posts: 23
Location: Iran

PostPosted: Thu Jul 24, 2014 2:52 pm    Post subject: Reply with quote

Hello again
I put together a little code to add it may be necessary
It can be ordered online using IP visit-online is stored in the file..
With a little knowledge of php language code any way you want to develop


Code:


@$file = file_get_contents('visit-online.txt');
@$array_1 = explode("\r\n", $file);
@$array_2 = array();
$count = 0;
//print_r($array_1);
foreach($array_1 as $key => $value){
   $array_2[$count] = explode('|', $array_1[$count]);
   $count++;   
}
//print_r($array_2);
$count = 0;
$result = NULL;
foreach($array_2 as $key => $value){
   if(!empty($value[0])){
      $result .= $value[0].'<br>';
   }
}
echo $result;

Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7366
Location: Lincoln Nebraska, USA

PostPosted: Thu Jul 24, 2014 7:52 pm    Post subject: Reply with quote

This reminds me of the old internet days using CGI (ie. perl) scripts to write to the file system for page counters Smile What happens with a LOT of concurrent writes to that .txt file? This is where something like SQLite would come in handy.
Back to top
View user's profile Send private message Visit poster's website
Aliresalat
Smarty Rookie


Joined: 09 Feb 2014
Posts: 23
Location: Iran

PostPosted: Fri Jul 25, 2014 1:30 am    Post subject: your welcome Reply with quote

mohrt wrote:
This reminds me of the old internet days using CGI (ie. perl) scripts to write to the file system for page counters Smile What happens with a LOT of concurrent writes to that .txt file? This is where something like SQLite would come in handy.


your welcome Very Happy Embarassed Very Happy
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger
Watchopolis
Smarty Rookie


Joined: 06 Aug 2014
Posts: 5

PostPosted: Wed Aug 06, 2014 8:46 pm    Post subject: Reply with quote

This is a nice plugin however the stats are wrong because when i have 15 visitor online on my website acording to google realtime stats
it shows 162 users online instead of 15 is there a way to fix this ?


Thank you for the plugin.
Back to top
View user's profile Send private message
Aliresalat
Smarty Rookie


Joined: 09 Feb 2014
Posts: 23
Location: Iran

PostPosted: Thu Aug 14, 2014 4:01 am    Post subject: Hello dear friend Reply with quote

Watchopolis wrote:
This is a nice plugin however the stats are wrong because when i have 15 visitor online on my website acording to google realtime stats
it shows 162 users online instead of 15 is there a way to fix this ?


Thank you for the plugin.


Hello dear friend
If you have some spare time can be greatly improved plug-in to fix the bugs
Of course, if you're familiar enough with php
I do not recommend this plugin for popular sites
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP