douxie1894 2019-04-20 03:47
浏览 53
已采纳

如何在PHP Codeigniter中使用cookie添加Post视图计数?

I am trying to add Post view counts using cookies in PHP Codeigniter. I have created a custom library(snippetfunctions) in which I have created a function(add_count) as shown below

  public function add_count($table,$column,$value)
{

$CI =& get_instance();//CI super object
$check_visitor = $CI->input->cookie(urldecode($value), FALSE);

 $ip = $CI->input->ip_address();
// if the visitor visit this article for first time then //
//set new cookie and update article_views column  ..
//you might be notice we used slug for cookie name and ip 
//address for value to distinguish between articles  views
if ($check_visitor == false) {
    $cookie = array(
        "name"   => urldecode($value),
        "value"  => "$ip",
        "expire" =>  time() + 0,
        "secure" => false
    );

    $CI->input->set_cookie($cookie);
$CI->Constant_model->update_counter($table,$column,urldecode($value));
}
}

and I used this function in my Controller file to add the count

$this->snippetfunctions->add_count('snippets','counter_views',$slug);

Where snippets is the table name, counter_views is the column name and $slug is the URL of the post which is used to identify the post.

The error I am getting is shown in image

enter image description here

Model function

 function update_counter($table, $column, $value)
{
    // return current article views 
    $this->db->where($column, urldecode($value));
    $this->db->select('counter_views');
    $count = $this->db->get($table)->row();
    // then increase by one 
    $this->db->where($column, urldecode($value));


    $this->db->set('counter_views', ($count['counter_views'] + 1));
    $this->db->update($table);
}
  • 写回答

2条回答 默认 最新

  • dongshuo1257 2019-04-20 05:31
    关注

    Try this code, hope this helps you
    3600 seconds and it means Current Time + 1 hour, your cookie will expire in one hour

    $cookie = array(
            "name"   => urldecode($value),
            "value"  => "$ip",
            "expire" =>  3600,//changes seconds for 1 hour from current time
            "secure" => false
        );
    

    Model:

    function update_counter($table, $column, $value)
    {
        // return current article views 
        $this->db->where($column, urldecode($value));
        $this->db->select('counter_views');
        $count = $this->db->get($table)->row('counter_views');
    
        $this->db->reset_query();
        // then increase by one 
        $this->db->set('counter_views', ($count + 1));//changes
        $this->db->where($column, urldecode($value));
        $this->db->update($table);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体