search
Homephp教程PHP源码PHP浏览次数统计类visit.class.php配合文本缓存

使用示例:
$visit = app::loader('visit'); 
$article_id = 20722; 
$visit->count('article', $article);

<?php

class visit
{

public $expires = array();
public $tables = array();


public function __construct()
{
$this->expires[&#39;article&#39;] = 10;
$this->tables[&#39;article&#39;] = &#39;pcb_article&#39;;

$this->expires[&#39;forum&#39;] = 9;
$this->tables[&#39;forum&#39;] = &#39;pcb_forum&#39;;

$this->expires[&#39;blog&#39;] = 8;
$this->tables[&#39;blog&#39;] = &#39;pcb_blog&#39;;
}



public function count($channel, $id)
{
$path = $this->get_cache_path($channel);

$this->update($path, $channel, $id);
}


private function get_cache_expires($channel)
{
$expires = isset($this->expires[$channel]) ? $this->expires[$channel] : 10;
return $expires;
}

private function get_cache_path($channel)
{
$time = time();
$minute = date(&#39;H&#39;, $time) * 60 + date(&#39;i&#39;, $time);
$expires = $this->get_cache_expires($channel);

$path = SYS_PATH ."/data/cache/visit/{$channel}". ceil($minute / $expires) .".txt";
return $path;
}


private function update($path, $channel, $id)
{
$dir = dirname($path);
if (!file_exists($dir))
make_dir($dir);

if (file_exists($path))
{
file_put_contents($path, &#39;,&#39; . $id, FILE_APPEND|LOCK_EX);
}
else
{
$table = $this->tables[$channel];
$mysql = app::loader(&#39;mysql&#39;);
$hand = opendir($dir);
while (($file = readdir($hand)))
{
if (preg_match("/{$channel}/", $file))
{
$visit = file_get_contents("{$dir}/{$file}");
$array = explode(&#39;,&#39;, $visit);
$array = array_count_values($array);
unset($array[&#39;&#39;]);

$sql = &#39;&#39;;
foreach ($array as $id => $visit)
{
$sql .="UPDATE `{$table}` SET `visit` = `visit` + {$visit} WHERE `id` = {$id} LIMIT 1;";
}
if ($sql)
{
$res = $mysql->executes($sql);
if ($res)
unlink($dir . &#39;/&#39; . $file);
}
unset($sql);
}
}

file_put_contents($path, &#39;,&#39; . $id, LOCK_EX);
}
}

}
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version