Thank you very much!
When reporting the bug I found a very old issue in the tracker, still open:
http://tracker.modx.com/issues/5294
I guess that is same bug? Or should I enter a new one?
Too bad I didn't find it earlier.
Thanks for the new faster way of doing the counter, works fine. I will use that way of working with tvs on other places too.
So, if someone would want it, here is a working code for a simple page hits counter, with a simple realod prevention:
$ip = $_SERVER['REMOTE_ADDR'];
$tvPageHitsId = 2;
$tvLatestIpId = 4;
$resId = $modx->resource->get('id');
$tvPh = $modx->getObject('modTemplateVarResource', array('tmplvarid' => $tvPageHitsId, 'contentid' => $resId));
$tvLip = $modx->getObject('modTemplateVarResource', array('tmplvarid' => $tvLatestIpId, 'contentid' => $resId));
if ($tvPh) {
$count = $tvPh->get('value');
if (!$count) { $count = 0; }
} else {
$tvPh = $modx->newObject('modTemplateVarResource', array('tmplvarid' => $tvPageHitsId, 'contentid' => $resId));
}
if ($tvLip) {
$latestIp = $tvLip->get('value');
} else {
$tvLip = $modx->newObject('modTemplateVarResource', array('tmplvarid' => $tvLatestIpId, 'contentid' => $resId));
}
if ($ip != $latestIp) {
$count++;
$tvPh->set('value', $count);
$tvPh->save();
}
$tvLip->set('value', $ip);
$tvLip->save();
return $count;
[ed. note: Uncle68 last edited this post 14 years, 7 months ago.]