diff options
| author | Eltrac <45323514+BigCoke233@users.noreply.github.com> | 2019-11-30 15:13:29 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-11-30 15:13:29 +0800 |
| commit | 9b1058952f4b473df92c1157ccb4153bb663b20c (patch) | |
| tree | e10596733846be5fe288d9ecded012b308c0e00e /functions.php | |
| parent | 224d2553e4cc82d7b561429594dbb1b2044fc50e (diff) | |
🔍 Add: 文章浏览数统计
Diffstat (limited to 'functions.php')
| -rw-r--r-- | functions.php | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/functions.php b/functions.php index cc26636..9490c0d 100644 --- a/functions.php +++ b/functions.php @@ -44,3 +44,34 @@ function themeVersion() { $info = Typecho_Plugin::parseInfo(__DIR__ . '/index.php'); return $info['version']; } + +/** + * 统计文章浏览数 + * from:http://docs.qqdie.com/ + */ +function get_post_view($archive) { + $cid = $archive->cid; + $db = Typecho_Db::get(); + $prefix = $db->getPrefix(); + if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) { + $db->query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT(10) DEFAULT 0;'); + echo 0; + return; + } + $row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid)); + if ($archive->is('single')) { + $views = Typecho_Cookie::get('extend_contents_views'); + if(empty($views)){ + $views = array(); + }else{ + $views = explode(',', $views); + } +if(!in_array($cid,$views)){ + $db->query($db->update('table.contents')->rows(array('views' => (int) $row['views'] + 1))->where('cid = ?', $cid)); +array_push($views, $cid); + $views = implode(',', $views); + Typecho_Cookie::set('extend_contents_views', $views); //记录查看cookie + } + } + echo $row['views']; +} |
