转换为语音并朗读全文 00:00 / 00:00

今天在这个基础上加入了cookie验证,让文章浏览次数更具有真实性。
在 functions.php 中加入下面代码

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'];
}

在需要显示次数的地方 (如 index.php,post.php,page.php) 加下边的代码

<?php get_post_view($this) ?>

来源:https://qqdie.com/archives/typecho-read-statistics.html

已有 3 条评论


  1. 今日新闻
    文章不错非常喜欢,支持
    今日新闻LV.1  20天前回复
  2. 知识共享网
    感谢分享,嘿嘿
    知识共享网LV.1  42天前回复
  3. Cialis
    Having read this I thought it was extremely enlightening.
    I appreciate you taking the time and effort to put this article together.
    I once again find myself spending a lot of time both reading and commenting.
    But so what, it was still worth it!
    CialisLV.1  44天前回复

发表新评论

© 2019 Powered by Typecho & Theme Quark