dstjh46606 2012-12-18 19:08
浏览 51
已采纳

如何记住Codeigniter中用户在会话中使用时间戳的10篇最后阅读文章?

I would like to make a PHP if condition code that will check if the last 10 articles or 10 minutes from the article reading by the user have already elapsed.

E.g.

A user open a page with id = 235 (this id value is in the url localhost/article/235 )

and this id value will be saved in session with a current timestamp and maybe his IP address

Then he read another article and the same will happen.

I need to remember the clicked stuff for another ten clicks and then reset that only for the first row. E.g. after the 10th click the id and timestamp will not became 11th row but will replace the 1st row in the list.

The php condition in CodeIgniter will then check these values and will update the article hit counter value in the articles table and column counter like this:

   $this->db->where('id', $id);
   $this->db->set('counter', 'counter+1', FALSE);
   $this->db->update('articles');

But before calling this code I need to make this check from the session?

How to do that?

I think storing e.g. 10 entries in the session with timestamps per user will be enough.

Just don't save the same page in the session twice.

And the condition will check the current timestamp with the saved one and if it is more than e.g. 10 minutes or the user have read/clicked another 10 articles it will allow the update counter php code.

I don't need to have this bulletproof. Just to disable the increment using browser's refresh button.

So, if he wants to increment the counter he will need to wait ten minutes or read another 10 articles ;)

  • 写回答

5条回答 默认 最新

  • doutuoben6908 2012-12-21 09:19
    关注

    Based on Raphael_ code and your question you can try this:

        <?php
                $aClicks = $this->session
                    ->userdata('article_clicks');
            $nextId = $this->session->userdata('nextId');
            // Initialize the array, if it's not already initialized
            if ($aClicks == false) {
                $aClicks = array();
                $nextId = 0;
            }
            // Now, we clean our array for the articles that have been clicked longer than
           // 10 minutes ago.
            $aClicks = array_filter($aClicks, function($click) {
                        return (time() - $click['time']) < 600; // Less than 10 minutes elapsed
                    }
            );
    // We check if the article clicked is already in the list
            $found = false;
            foreach ($aClicks as $click) {
                if ($click['article'] === $id) { // Assuming $id holds the article id
                    $found = true;
                    break;
                }
            }
    // If it's not, we add it
            if (!$found) {
                $aClicks[$nextId] = array(
                    'article' => $id, // Assuming $id holds the article id
                    'time' => time()
                );
                $nextId++;
                $this->session->set_userdata('nextId', $nextId);
            }
            $this->session->set_userdata('article_clicks', $aClicks);
            if (count($aClicks) > 10 && $nextId > 9) {
                $this->session->set_userdata('nextId', 0);
                echo "OK!";
    
            }
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line