dpa84373 2014-01-28 15:30
浏览 24

PHP会话以防止viewcounter上的错误视图

I have a view counter on my blog which tracks how many times certain article has been viewed, however, this plugin that I use allows for endless hits simply by pressing F5.

I'm trying to edit the plugin so that it can only count one view per page visit.

I already created a cookie to prevent this from happening, which seems to work fine. Of course cookies could easily be disabled. This is why I tried to achieve the same thing using a PHP session.

Here is my code that, for some reason, doesn't seem to be working (the cookie works perfectly though):

session_start();
    if(($_COOKIE['last_ip_address_' . $id]!= $_SERVER['REMOTE_ADDR'] . '_' . $id) 
    || ($_SESSION['last_ip_address_' . $id]!= $_SERVER['REMOTE_ADDR'] . '_' . $id)) {
        if(!update_post_meta($id, 'views', ($post_views+1))) 
            add_post_meta($id, 'views', 1, true);
        }
        $_SESSION['last_ip_address_' . $id] = $_SERVER['REMOTE_ADDR'] . '_' . $id;  
        setcookie('last_ip_address_' . $id, $_SERVER['REMOTE_ADDR'] . '_' . $id, time()+3600);
        }
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
    • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
    • ¥15 运动想象脑电信号数据集.vhdr
    • ¥15 三因素重复测量数据R语句编写,不存在交互作用
    • ¥15 微信会员卡等级和折扣规则
    • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒