dousong1926 2014-07-03 11:32
浏览 38
已采纳

Laravel 4:独特访客数量的Cookie

I am creating a simple blog in which a user can add, update and view posts. I have implemented the views count functionality in the post that shows the number of views on the post. For that, what I did is:

  1. Created an event listener:

    Event::listen('post.viewed', 'PostHandler@updatePostViewsAction');

  2. Created the PostHandler and updatePostViewsAction

    class PostHandler
    {
        public function handle()
        {
            // 
        }
    
        public function updatePostViewsAction( $post )
        {
            // Update view counter of post
            $post->views_count = $post->views_count + 1;
            $post->save();
        }
    }
    

This is working fine and the views count is succesfuly being updated. But later on I decided to make the views to be uniquely counted. For this, I have tried using the cookies i.e. create a cookie on users computer, whenever he views a post and increment the views_count . If the users comes back again and views the post again, check if there is a cookie available, if it is available then don't increment the views_count, otherwise increment that. Below is, how I have implemented this:

class PostHandler
{
    public function handle()
    {
        // 
    }

    public function updatePostViewsAction( $post )
    {
        if ( !Cookie::get('post_viewed') ) {
            // Update view counter of post
            $post->views_count = $post->views_count + 1;
            $post->save();
            Cookie::forever('post_viewed', true);
        }
    }
}

But it doesn't seem to work, as the views_count is getting incremented each and every time. Can anyone please tell me, what am I doing wrong here?

  • 写回答

1条回答 默认 最新

  • doushang8512 2014-07-04 06:20
    关注

    In order to save a cookie with Laravel, you are required to send it to the response. However, you can workaround it by sending the cookie to the queue.

    public function updatePostViewsAction( $post )
    {
        if ( !Cookie::get('post_viewed') ) {
            // Update view counter of post
            $post->views_count = $post->views_count + 1;
            $post->save();
            // Create a cookie before the response and set it for 30 days
            Cookie::queue('post_viewed', true, 60 * 24 * 30);
        }
    }
    

    Source from Laravel Docs http://laravel.com/docs/requests#cookies:

    Queueing A Cookie For The Next Response

    If you would like to set a cookie before a response has been created, use the Cookie::queue() method. The cookie will automatically be attached to the final response from your application.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'