Laravel,如何使用cookie在博客上创建文章读者

时间:2017-07-13 15:51:11

标签: php laravel cookies

我有一个这样的脚本:

<?
    public function read($slug, $read_more) {
        $artikel     = App\Post::where('read_more', $read_more)->first();
        $comments    = App\Comment::where('post_id', $artikel->id)->where('active', '1')->get();
        $pops        = App\Post::orderBy('count', 'desc')->limit(5)->get();
        $coms        = App\Comment::orderBy('active', 'desc')->orderBy('created_at', 'desc')->limit(5)->get();
        $categorys   = App\Category::all();
        $title       = 'Artikel ' . $artikel->category->category . ' &raquo; ' . $artikel->title;
        $description = str_limit($artikel->content, 70);
        $keywords    = $artikel->category->category . ', teknologi, komputer, artikel, pemrograman, informasi, terbaru, linux';
        $view        = view('guest.read', compact('artikel', 'comments', 'categorys', 'title', 'description', 'keywords', 'pops', 'coms'));
        if (Cookie::get('__urda') == $artikel->id) {
            return $view;
        } else {
            $count        = App\Post::find($artikel->id);
            $count->count = $artikel->count + 1;
            $count->save();
            $cookie = new Response($view);
            $cookie->withCookie(cookie()->forever('__urda', $artikel->id));
            return $cookie;
        }
    }
?>

阅读文章时出现问题。

如果您阅读文章A,读者人数会增加。

如果您阅读文章B,读者人数会增加。

如果您重读文章A,读者人数将再次增加。 (不应该增加)

示例:http://www.jinggacloud.com

解决方案是什么

1 个答案:

答案 0 :(得分:0)

您还可以在Cookie中存储一个包含所有阅读书籍的数组。但是用户可以删除cookie。另一种解决方案是存储的数据库表,ip打开视频。也许你可以匹配这些想法,以改善你的计数。