Home >Backend Development >PHP Tutorial >php 怎么统计文章的访问量?

php 怎么统计文章的访问量?

WBOY
WBOYOriginal
2016-06-06 20:10:041417browse

php 怎么统计文章的访问量,需要考虑刷新

回复内容:

php 怎么统计文章的访问量,需要考虑刷新

这个问题问的还有不够明确啊!你的统计条件什么?
1、是刷新一次统计一次?
2、根据IP统计?(如果是IP的话,如果在同一局域网下同一个IP不同的访问者访问那统计还是有误,建议获取客户端IP为准)

第1种情况,直接在对应数据表中创建浏览量的统计字段即可,每次访问SQL+1操作即可

<code>example SQL:
UPDATE article SET Views=Views+1 WHERE articleId=xxx</code>

第2中情况,就需要根据获取的IP统计了

<code>example PHP:
/**
 * 获取客户端IP
 */
function getClientIp(){
    if(isset($_SERVER["HTTP_CLIENT_IP"]) and strcasecmp($_SERVER["HTTP_CLIENT_IP"], "unknown")){
        return $_SERVER["HTTP_CLIENT_IP"];
    }
    if(isset($_SERVER["HTTP_X_FORWARDED_FOR"]) and strcasecmp($_SERVER["HTTP_X_FORWARDED_FOR"], "unknown")){
        return $_SERVER["HTTP_X_FORWARDED_FOR"];
    }
    if(isset($_SERVER["REMOTE_ADDR"])){
        return $_SERVER["REMOTE_ADDR"];
    }
    return "";
}</code>

希望对你有所帮助^_^

用Redis,访问一次就increase一次

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