Emlog评论获取并显示评论者的操作系统与浏览器信息,显示方式为小图标加文字说明。实现方法如下:

1.进入博客根目录的include/model/文件夹,打开comment_model.php文件,找到:

while ($row = $this->db->fetch_array($ret)) {
$row['poster'] = htmlspecialchars($row['poster']);
$row['mail'] = htmlspecialchars($row['mail']);
$row['url'] = htmlspecialchars($row['url']);
$row['content'] = htmlClean($row['comment']);
$row['date'] = smartDate($row['date']);
$row['children'] = array();

在以上代码后面另起一行增加一个获取useragent信息的代码:

$row['useragent'] = htmlspecialchars($row['useragent']);

2.接着在该文件找到添加评论的代码:

function addComment($name, $content, $mail, $url, $imgcode, $blogId, $pid) 
{
$ipaddr = getIp();
$utctimestamp = time();

if($pid != 0) {
$comment = $this->getOneComment($pid);
$content = '@' . addslashes($comment['poster']) . ':' . $content;
}

$ischkcomment = Option::get('ischkcomment');
$hide = ROLE == ROLE_VISITOR ? $ischkcomment : 'n';

$sql = 'INSERT INTO '.DB_PREFIX."comment (date,poster,gid,comment,mail,url,hide,ip,pid)
VALUES ('$utctimestamp','$name','$blogId','$content','$mail','$url','$hide','$ipaddr','$pid')";
$ret = $this->db->query($sql);
$cid = $this->db->insert_id();
$CACHE = Cache::getInstance();

if ($hide == 'n') {
$this->db->query('UPDATE '.DB_PREFIX."blog SET comnum = comnum + 1 WHERE gid='$blogId'");
$CACHE->updateCache(array('sta', 'comment'));
doAction('comment_saved', $cid);
emDirect(Url::log($blogId).'#'.$cid);
} else {
$CACHE->updateCache('sta');
doAction('comment_saved', $cid);
emMsg('评论发表成功,请等待管理员审核', Url::log($blogId));
}
}

修改为(下划线部分):

function addComment($name, $content, $mail, $url, $imgcode, $blogId, $pid) 
{
$ipaddr = getIp();
$useragent = $_SERVER['HTTP_USER_AGENT'];
$utctimestamp = time();

if($pid != 0) {
$comment = $this->getOneComment($pid);
$content = '@' . addslashes($comment['poster']) . ':' . $content;
}

$ischkcomment = Option::get('ischkcomment');
$hide = ROLE == ROLE_VISITOR ? $ischkcomment : 'n';

$sql = 'INSERT INTO '.DB_PREFIX."comment (date,poster,gid,comment,mail,url,hide,ip,pid,useragent)
VALUES ('$utctimestamp','$name','$blogId','$content','$mail','$url','$hide','$ipaddr','$pid','$useragent')";
$ret = $this->db->query($sql);
$cid = $this->db->insert_id();
$CACHE = Cache::getInstance();

if ($hide == 'n') {
$this->db->query('UPDATE '.DB_PREFIX."blog SET comnum = comnum + 1 WHERE gid='$blogId'");
$CACHE->updateCache(array('sta', 'comment'));
doAction('comment_saved', $cid);
emDirect(Url::log($blogId).'#'.$cid);
} else {
$CACHE->updateCache('sta');
doAction('comment_saved', $cid);
emMsg('评论发表成功,请等待管理员审核', Url::log($blogId));
}
}

3.进入博客根目录,进入模板根目录:content/templates/当前使用模板名称(如default),打开module.php文件,在最后加入:

<?php
//获取评论用户操作系统、浏览器等信息
function useragent($info){
require_once 'useragent.class.php';
$useragent = UserAgentFactory::analyze($info);
?>
<img src="<?php echo TEMPLATE_URL.$useragent->platform['image']?>"> <?php echo $useragent->platform['title']; ?> 
<img src="<?php echo TEMPLATE_URL.$useragent->browser['image']?>"> <?php echo $useragent->browser['title']; ?>
<?php
}
?>

如果不需要文字说明,可替换成下面这个:

<?php
//获取评论用户操作系统、浏览器等信息
function useragent($info){
require_once 'useragent.class.php';
$useragent = UserAgentFactory::analyze($info);
?>
<img src="<?php echo TEMPLATE_URL.$useragent->platform['image']?>">
<img src="<?php echo TEMPLATE_URL.$useragent->browser['image']?>">
<?php
}
?>

显示效果如图:

emlog评论显示操作系统浏览器

4.继续在module.php文件里的评论列表和子评论列表适当位置(如<?php echo $comment['poster']; ?>后面)加入以下调用代码

<?php echo useragent($comment['useragent']); ?>

5.下载下面的附件,上传并解压到你当前使用模板主题的根目录(见步骤3)

磨盘下载: http://m.xiaows.com/file/346645

如有疏漏,请留言。

本文由笑忘书整理自独狼博客: http://www.xlonewolf.net/work/280.html

全文完