我想做一个PHP条件代码,它将检查用户阅读文章的最后10篇文章或10分钟后是否已经过去了。
例如。
用户使用id =235个打开页面(该id值位于url //
这个id值将在与当前时间戳的会话中保存,并可能他的IP地址。
然后他又读了一篇文章,同样的事情也会发生。
,我需要记住点击的东西,再点击10次,然后只对第一行重新设置。例如,在第10次单击id之后,时间戳不会变成第11行,而是将替换列表中的第1行。。
然后,CodeIgniter中的php条件将检查这些值,并更新文章命中的计数器值和列计数器,如下所示:
$this->db->where('id', $id);
$this->db->set('counter', 'counter+1', FALSE);
$this->db->update('articles');但在调用此代码之前,我需要从会话中进行检查吗?
怎么做?
我认为,在会话中存储10个条目,每个用户都有时间戳就足够了。
只是不要在会话中保存相同的页面两次。
该条件将使用保存的时间戳检查当前时间戳,如果时间超过10分钟,或者用户已读取/单击了另外10篇文章,则将允许更新计数器php代码。
我不需要这个防弹衣。使用浏览器的刷新按钮禁用增量。
因此,如果他想增加计数器,他需要等待10分钟,或者再读10篇文章;);)
发布于 2012-12-21 09:19:47
根据Raphael_代码和您的问题,您可以尝试如下:
<?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!";
}
?>发布于 2012-12-18 19:57:10
你绝对应该参加会议。它节省了您的带宽消耗和更容易处理。当然,除非你需要客户端的数据,根据你的解释,我认为你不需要,假设你去了会议,你所要做的就是用你所拥有的数据存储一个数组。下面的代码应该这样做:
$aClicks = $this->session
->userdata('article_clicks');
// Initialize the array, if it's not already initialized
if ($aClicks == false) {
$aClicks = array();
}
// 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[] = array(
'article' => $id, // Assuming $id holds the article id
'time' => time()
);
}
// Store the clicks back to the session
$this->session
->set_userdata('article_clicks', $aClicks);
// If we meet all conditions
if (count($aClicks) < 10) {
// Do something
}发布于 2012-12-22 14:42:31
我假设$clicks是一个数组,最多有10篇访问过的文章。id用作键,时间戳用作值。$id是新文章的id。
$clicks = $this->session->userdata('article_clicks');
//default value
$clicks = ($clicks)? $clicks : array();
//could be loaded from config
$maxItemCount = 10;
$timwToLive= 600;
//helpers
$time = time();
$deadline = $time - $timeToLive;
//add if not in list
if(! isset($clicks[$id]) ){
$clicks[$id] = $time;
}
//remove old values
$clicks = array_filter($clicks, function($value){ $value >= $deadline;});
//sort newest to oldest
arsort($clicks);
//limit items, oldest will be removed first because we sorted the array
$clicks = array_slice($clicks, 0, $maxItemCount);
//save to session
$this->session->>set_userdata('article_clicks',$clicks)用法:
//print how mch time has passed since the last visit
if(isset($clicks[$id]){
echo "visited ".($time-$clicks[$id]). "seconds ago." ;
} else {
echo "first visit";
}编辑:您必须使用arsort不重新排序,否则键会丢失,对不起
https://stackoverflow.com/questions/13939786
复制相似问题