Cookie会对性能产生负面影响吗?

时间:2016-01-07 22:55:48

标签: php performance session cookies

我正在为我的网站创建一个原始视图计数器,它将更新数据库,然后使用页面ID设置一个cookie,这样就不会插入另一个视图。今天我意识到,用户可以很快拥有大量的cookie,具体取决于他们在网站上访问的页数。是否有更好的方法只为每个用户插入一个视图,或者这种cookie方法是否足够。感谢。

PHP查看更新代码:

public function updateViews ($id) {
    $db = Database::getInstance();

    //Set Cookie to indicate viewed
    setcookie($id, rand(), time()+3600*24*365*10);

    //Set required variables
    $currentViews = $this->getData($id)->fetch(PDO::FETCH_ASSOC)['views'];
    $newViews = $currentViews + 1;

    if (isset($_COOKIE[$id])) {
        //Do nothing because cookie isset
    } else {
        //Define Query
        $query = "UPDATE coils SET `views` = '".$newViews."' WHERE `uniqueid` = '".$id."'"; 

        //Prepare Query
        $prepareQuery = $db->getConnection()->prepare($query);

        //Execute Query
        $prepareQuery->execute();
    }

    $currentViews = number_format($currentViews);


    //List results to user
    if ($currentViews > 1) {
        $this->views = $currentViews . " Views";
    } else if ($currentViews <= 1) {
        $this->views = $currentViews . " View";
    }
}

1 个答案:

答案 0 :(得分:0)

在阅读了会话和cookie后,我认为数据库是更好的解决方案。它允许我阻止来自不同浏览器的多个视图,并且比仅仅cookie或会话更准确。