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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看