效果
Content.php添加
- 将以下代码加到/usr/themes/handsome/libs/Content.php中,放在class Content的上面
/*访问总量*/
function theAllViews(){
$db = Typecho_Db::get();
$row = $db->fetchAll('SELECT SUM(VIEWS) FROM `typecho_contents`');
echo number_format($row[0]['SUM(VIEWS)']);
}
/*响应时间*/
function timer_start() {
global $timestart;
$mtime = explode( ' ', microtime() );
$timestart = $mtime[1] + $mtime[0];
return true;
}
timer_start();
function timer_stop( $display = 0, $precision = 3 ) {
global $timestart, $timeend;
$mtime = explode( ' ', microtime() );
$timeend = $mtime[1] + $mtime[0];
$timetotal = number_format( $timeend - $timestart, $precision );
$r = $timetotal < 1 ? $timetotal * 1000 . " ms" : $timetotal . " s";
if ( $display ) {
echo $r;
}
return $r;
}
/*全站字数*/
function allOfCharacters() {
$chars = 0;
$db = Typecho_Db::get();
$select = $db ->select('text')->from('table.contents');
$rows = $db->fetchAll($select);
foreach ($rows as $row) { $chars += mb_strlen(trim($row['text']), 'UTF-8'); }
$unit = '';
if($chars >= 10000) { $chars /= 10000; $unit = '万'; }
else if($chars >= 1000) { $chars /= 1000; $unit = '千'; }
$out = sprintf('%.2lf %s',$chars, $unit);
return $out;
}
/*在线人数*/
function online_users() {
$filename='online.txt'; //数据文件
$cookiename='Nanlon_OnLineCount'; //Cookie名称
$onlinetime=30; //在线有效时间
$online=file($filename);
$nowtime=$_SERVER['REQUEST_TIME'];
$nowonline=array();
foreach($online as $line){
$row=explode('|',$line);
$sesstime=trim($row[1]);
if(($nowtime - $sesstime)<=$onlinetime){
$nowonline[$row[0]]=$sesstime;
}
}
if(isset($_COOKIE[$cookiename])){
$uid=$_COOKIE[$cookiename];
}else{
$vid=0;
do{
$vid++;
$uid='U'.$vid;
}while(array_key_exists($uid,$nowonline));
setcookie($cookiename,$uid);
}
$nowonline[$uid]=$nowtime;
$total_online=count($nowonline);
if($fp=@fopen($filename,'w')){
if(flock($fp,LOCK_EX)){
rewind($fp);
foreach($nowonline as $fuid=>$ftime){
$fline=$fuid.'|'.$ftime."\n";
@fputs($fp,$fline);
}
flock($fp,LOCK_UN);
fclose($fp);
}
}
echo "$total_online";
}
sidebar.php添加
- 找到/usr/themes/handsome/component/sidebar.php文件,找到合适位置添加代码
我放的位置是
<!--博客信息-->
<?php if (@!in_array('info', Utils::checkArray($this->options->sidebarSetting))): ?>
<section id="blog_info" class="widget widget_categories wrapper-md clear">
<h5 class="widget-title m-t-none text-md"><?php _me("博客信息") ?></h5>
下方添加如下代码:
<!--自定义统计-->
<li class="list-group-item text-second">
<span class="blog-info-icons">
<i data-feather="edit-3">
</I>
</span>
<span class="badge pull-right">
<?php echo allOfCharacters(); ?>
</span>
<?php _me("全站字数") ?>
</li>
<li class="list-group-item text-second">
<span class="blog-info-icons">
<i data-feather="users">
</i>
</span>
<span class="badge pull-right">
<?php echo theAllViews(); ?>
</span>
<?php _me("访客总数") ?>
</li>
<li class="list-group-item">
<i class="glyphicon glyphicon-user text-muted text-muted">
</i>
<span class="badge pull-right">
<?php echo online_users() ?>
</span>
<?php _me("在线人数") ?>
</li>
<li class="list-group-item text-second">
<span class="blog-info-icons">
<i data-feather="refresh-ccw">
</i>
</span>
<span class="badge pull-right">
<?php echo timer_stop(); ?>
</span>
<?php _me("响应耗时") ?>
</li>