ScottW23
11-18-2009, 03:00 AM
In vb 3.8.4, what do you think would be the easiest/simplest way to make it so that the thread view count is only incremented for each unique user? In other words, hitting refresh on a thread page wouldn't increment the count, unless the user changed IP's.
Would I need to maintain a database of IP's and check the table before incrementing the count, or is there a simpler way? I could use cookies to do it, but if someone disabled their cookies, then they could just increment the count at will.
Nevermind I decided to just code it. This goes in showthread.php around line 489 replacing the existing views counter code.
$userip = $_SERVER[REMOTE_ADDR];
$ipnum = ip_address_to_number($userip);
$getuip = $db->query_first("
SELECT count(*) as views
FROM ipchecker
WHERE threadid = ".intval($threadinfo['threadid'])." AND ipaddr=".$ipnum
);
if ($getuip['views'] <=1) {
if ($vbulletin->options['threadviewslive'])
{
// doing it as they happen; for optimization purposes, this cannot use a DM!
$db->shutdown_query("
UPDATE " . TABLE_PREFIX . "thread
SET views = views + 1
WHERE threadid = " . intval($threadinfo['threadid'])
);
}
else
{
// or doing it once an hour
$db->shutdown_query("
INSERT INTO " . TABLE_PREFIX . "threadviews (threadid)
VALUES (" . intval($threadinfo['threadid']) . ')'
);
$db->shutdown_query("
INSERT INTO ipchecker VALUES (NULL,".time().",".$ipnum.",".intval($threadinfo['threadid']).')'
);
}
}
function ip_address_to_number($IPaddress)
{
if ($IPaddress == "") {
return 0;
} else {
$ips = split ("\.", "$IPaddress");
return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
}
}
Also need to create the table for the IP's:
CREATE TABLE `ipchecker` (
`id` int(10) NOT NULL auto_increment,
`datestamp` int(10) NOT NULL default '0',
`ipaddr` int(10) NOT NULL default '0',
`threadid` int(10) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `ipcheck` (`threadid`,`ipaddr`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
And I setup a separate cron to delete any row older than 24 hours to declutter the table. So it restricts views to 1 unique IP per day.
Would I need to maintain a database of IP's and check the table before incrementing the count, or is there a simpler way? I could use cookies to do it, but if someone disabled their cookies, then they could just increment the count at will.
Nevermind I decided to just code it. This goes in showthread.php around line 489 replacing the existing views counter code.
$userip = $_SERVER[REMOTE_ADDR];
$ipnum = ip_address_to_number($userip);
$getuip = $db->query_first("
SELECT count(*) as views
FROM ipchecker
WHERE threadid = ".intval($threadinfo['threadid'])." AND ipaddr=".$ipnum
);
if ($getuip['views'] <=1) {
if ($vbulletin->options['threadviewslive'])
{
// doing it as they happen; for optimization purposes, this cannot use a DM!
$db->shutdown_query("
UPDATE " . TABLE_PREFIX . "thread
SET views = views + 1
WHERE threadid = " . intval($threadinfo['threadid'])
);
}
else
{
// or doing it once an hour
$db->shutdown_query("
INSERT INTO " . TABLE_PREFIX . "threadviews (threadid)
VALUES (" . intval($threadinfo['threadid']) . ')'
);
$db->shutdown_query("
INSERT INTO ipchecker VALUES (NULL,".time().",".$ipnum.",".intval($threadinfo['threadid']).')'
);
}
}
function ip_address_to_number($IPaddress)
{
if ($IPaddress == "") {
return 0;
} else {
$ips = split ("\.", "$IPaddress");
return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
}
}
Also need to create the table for the IP's:
CREATE TABLE `ipchecker` (
`id` int(10) NOT NULL auto_increment,
`datestamp` int(10) NOT NULL default '0',
`ipaddr` int(10) NOT NULL default '0',
`threadid` int(10) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `ipcheck` (`threadid`,`ipaddr`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
And I setup a separate cron to delete any row older than 24 hours to declutter the table. So it restricts views to 1 unique IP per day.