簡體   English   中英

PHP 只有點擊計數器?

[英]PHP only Hit Counter?

所以,我曾經在很多年前玩過 web 開發,我有點生疏,但不是那么生疏!

前幾天我剛剛制作了一個完整的視頻游戲頁面,使用 PHP 變量將不同的游戲加載到不同大小的 iframe 中。 話雖如此,為什么我不能讓一個簡單的 PHP 計數器工作呢? 我一個接一個地下載腳本,CHMOD'ed txt 文件到 777,整個 9。Chrome 不支持點擊計數器之類的嗎? 似乎我訪問的那些通過它們提供點擊計數器的網站在他們的演示頁面上不起作用?,這是怎么回事,我記得幾年前,我復制了大約 10 行非常基本的代碼,將其保存為 PHP 文件,將它與一個空白的 txt 文件一起上傳到服務器。 砰? 每次都完美地工作。 發生了什么變化?

這是我正在使用的代碼。 順便一提。 我嘗試將它添加到我的 index.html 中,我還嘗試將它用作單獨的 php 文件並使用 INCLUDE 調用它,一切。 似乎沒有任何效果。

<?php

$open = fopen(“hits.txt”, “r+”);
$value = fgets($open);
$close = fclose($open);

$value++;

$open = fopen(“hits.txt”, “w+”);
fwrite($open, $value); // variable is not restated, bug fixed.
$close = fclose($open);

?>

然后我想要顯示結果的地方,我有,

<?php echo $value; ?>

有任何想法嗎?

您可以將以下內容用作基本命中計數器:

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

您可能想要實現某種方法來檢查它不僅僅是一個用戶刷新頁面......一些簡單的東西:

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

$_SESSION['visited'] = true;

將檢查用戶是否已在同一session訪問過該站點,如果訪問過,則不會增加該值。

您可以在 PHP 中創建一個 ajax 文件,並在一個時間間隔內調用 ajax 並顯示點擊計數器到頁面。

為此,首先創建一個 ajax 文件:ajax_user_count.php

<?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;

然后通過這個調用ajax文件

$(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。 想出了這個易於實現的計數器。 一探究竟

<?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);

?>

希望這段代碼對某人有用。

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

它將一個字節附加到一個文件並讀取文件大小作為計數器。 這是我在 0.1 毫秒內發現的最快且無錯誤的邏輯

這計算初始點擊(不是刷新)並按日期保存到 json 文件:

<?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));    
}

?>

刷新檢查器來自PHP:Detect Page Refresh

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM