FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » FUDforum » How To » th_inc_view_count() (Only increments if user is logged in)
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
th_inc_view_count() [message #162579] Sat, 12 June 2010 02:18 Go to next message
art is currently offline  art   
Messages: 35
Registered: June 2009
Karma: 0
Member
I have a question. I have noticed that the function th_inc_view_count($id) only increments the view count if the user viewing the thread is logged in. I guess this helps weed out search bots but it also weeds out about 95% of the human readers on my forum. How can I set it to increment the count even if the user isn't logged in?
Re: th_inc_view_count() [message #162580 is a reply to message #162579] Sat, 12 June 2010 07:09 Go to previous messageGo to next message
naudefj is currently offline  naudefj   South Africa
Messages: 3771
Registered: December 2004
Karma: 28
Senior Member
Administrator
Core Developer
Hi Tesla,

Looking at the code all readers will increment the count (even anonymous & bots). What we actually need is some caching to prevent every view from hitting the database.

Best regards.

Frank
Re: th_inc_view_count() [message #162581 is a reply to message #162580] Sat, 12 June 2010 08:41 Go to previous messageGo to next message
art is currently offline  art   
Messages: 35
Registered: June 2009
Karma: 0
Member
That doesn't appear to be the case. While logged in here, for example, if I hit refresh, the view count increments. If I log out and click refresh, it doesn't. I've tried this on several computers, so it doesn't appear to be a caching issue, local to the client. The behavior is the same when the browser is on a Windows box running IE as it is on a Linux box running Firefox.
Re: th_inc_view_count() [message #162582 is a reply to message #162581] Sat, 12 June 2010 10:24 Go to previous messageGo to next message
naudefj is currently offline  naudefj   South Africa
Messages: 3771
Registered: December 2004
Karma: 28
Senior Member
Administrator
Core Developer
Open 2 different types of web browsers on your PC (say Firefox and IE).

In one: LOGIN, visit a topic and note the counter was increased.
In the second: DO NOT log in, visit a topic and note the counter was increased as well.

Now, if you log out from the first, the counter will not be refreshed any more. Not sure why, but I guess it's because of caching.

Best regards.

Frank
Re: th_inc_view_count() [message #162583 is a reply to message #162582] Sat, 12 June 2010 14:52 Go to previous messageGo to next message
art is currently offline  art   
Messages: 35
Registered: June 2009
Karma: 0
Member
That's kind of my point. It's weird, isn't it? Viewers that aren't logged in cannot update the database for some reason.

I put this in th.inc.t:

function th_inc_view_count($id)
{
q('UPDATE {SQL_TABLE_PREFIX}thread SET views=views+1 WHERE id='.$id);
$result = q_singleval('SELECT views from {SQL_TABLE_PREFIX}thread where id='.$id);
print("<br>count=$result");
}

And I could see that the update simply doesn't work unless the user is logged in on the computer. I assume it's permission problems, or something in the fud_sql_lnk connection and possibly the variables set in GLOBALS preventing writes if the user isn't logged in. But I thought I'd ask before looking further.

I tried this also, with no success:

function th_inc_view_count($id)
{
$query = 'UPDATE {SQL_TABLE_PREFIX}thread SET views=views+1 WHERE id='.$id;
$result = mysql_query($query, fud_sql_lnk) or die (fud_sql_error_handler($query, mysql_error(fud_sql_lnk), mysql_errno(fud_sql_lnk), get_version()));
$result = q_singleval('SELECT views from {SQL_TABLE_PREFIX}thread where id='.$id);
print("<br>count=$result");
}

I did that thinking it may have been in the "q" function, but realized that this second form was essentially the same thing. That's what makes me think it is in the fud_sql_lnk connection perhaps.

Any other ideas?
Re: th_inc_view_count() [message #162584 is a reply to message #162583] Sat, 12 June 2010 20:06 Go to previous messageGo to next message
naudefj is currently offline  naudefj   South Africa
Messages: 3771
Registered: December 2004
Karma: 28
Senior Member
Administrator
Core Developer
It's definitely caching. Look at __fud_cache() in include/core.inc.

It's not that the UPDATE doesn't work - it is not executed. Right or wrong, FUDforum instructs the browser to render the page from browser cache and the code is not executed.
Re: th_inc_view_count() [message #162585 is a reply to message #162584] Sat, 12 June 2010 20:44 Go to previous messageGo to next message
art is currently offline  art   
Messages: 35
Registered: June 2009
Karma: 0
Member
I will look there, thanks.
Re: th_inc_view_count() [message #162587 is a reply to message #162585] Sat, 12 June 2010 23:33 Go to previous messageGo to next message
art is currently offline  art   
Messages: 35
Registered: June 2009
Karma: 0
Member
Thanks for your help, Frank, you were right.

No coding change was needed, it was a config setting:

{{{DISABLE_ANON_CACHE FUD_OPT_3 65536
Disable sending of page-cache headers to anonymous users, this may solve extended caching by browsers non-complaint with web specs.}}}
icon6.gif  Re: th_inc_view_count() [message #162595 is a reply to message #162587] Mon, 14 June 2010 20:50 Go to previous message
The Witcher is currently offline  The Witcher   United States
Messages: 675
Registered: May 2009
Location: USA
Karma: 3
Senior Member
The config setting is:

Admin Control Panel: > Global Settings Manager: > User Account Settings: >
Disable caching for anonymous users:
Disable sending of page-cache headers to anonymous users, this may solve extended caching by browsers non-complaint with web specs.

(If set to yes then anonymous users will be counted in the page views, if set to no then anonymous views will not be counted.)

It just depends on if you want every view to be counted or just those who are registered/logged in.


"I'm a Witcher, I solve human problems; not always using a sword!"
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Merging forums
Next Topic: Cookie domain
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 11:48:51 GMT 2024

Total time taken to generate the page: 0.02460 seconds