PHP only Hit Counter?



PHP Snippet 1:

$counter = file_get_contents('./file') + 1;
file_put_contents('./file', $counter);

PHP Snippet 2:

session_start();
if(empty($_SESSION['visited'])){
    $counter = file_get_contents('./file') + 1;
    file_put_contents('./file', $counter);
}

$_SESSION['visited'] = true;

PHP Snippet 3:

<?php
session_start();
$log_file_name = 'traffic_count.log';
$count = file_get_contents($log_file_name, true);
if(empty($count)){$count = 0;}  
if(isset($_POST['action'])){
    $action = $_POST['action'];
    if($action == 'enter'){
        if(!isset($_SESSION['user_count'])){
            $count += 1;
            if($count == 0) { $count = 1; }
            $_SESSION['user_count'] = $count;
            $message = $count; 
            file_put_contents($log_file_name, $message);        
        }
    } else if($action == 'leave'){
        $count -= 1;
        $_SESSION['user_count'] = $count;
        $message = $count; 
        file_put_contents($log_file_name, $message);
        session_destroy();  
    }
}

echo $count;
die;

PHP Snippet 4:

$(document).ready(function(){
    get_enter_web_traffic();
    setInterval(function(){ 
        get_enter_web_traffic();
    }, 1000);
    $(window).bind("beforeunload", function() {
        $.ajax({
                type: "POST",
                async: false,
                cache: false,
                url: "ajax_user_count.php",
                data: {'action' : 'leave'},
                success: function(result) { },
                error: function(data) { location.reload(); 
            }
        });
    });     

});

function get_enter_web_traffic(){
    var data = {'action' : 'enter'};    
    $.post('ajax_user_count.php',data,function(response){
        $('#count_user').html(response); // website hit count
    }); 
}

PHP Snippet 5:

<?php 

$filename = "count.txt";// the text file to store count
// Open the file foe reading current count
$fp = fopen($filename, 'r');

//Get exiting count
$count = fread($fp, filesize($filename));

//close file
fclose($fp);

//Add 1 to the existing count
$count = $count +1;

//Display the number of hits
echo "<p>Total amount of Hits:" . $count. "</p>";

//Reopen to modify content
$fp = fopen($filename, 'w');

//write the new count to file
fwrite($fp, $count);

//close file
fclose($fp);

?>

PHP Snippet 6:

file_put_contents('count.txt',"\n",FILE_APPEND|LOCK_EX);
$count = filesize("count.txt"); 

PHP Snippet 7:

<?php

$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
if (!$pageWasRefreshed) { #ignore page refresh
    $today = date("Y/m/d");
    $filename = 'traffic.json';
    $traffic = json_decode(file_get_contents($filename), true);
    $traffic[$today] += 1;
    file_put_contents($filename, json_encode($traffic));    
}

?>