Is this possible?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
turulojko
Forum Newbie
Posts: 6
Joined: Fri Feb 22, 2008 6:45 am

Is this possible?

Post by turulojko »

Ex. I have site with posts. When someone view some post, the post views counts +1.

What i want...
is it possible to count the post views +1 only when the visitros view that post for 10 or more seconds?
if its possible, how can i do that?

Sorry for my bad english.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Is this possible?

Post by califdon »

turulojko wrote:Ex. I have site with posts. When someone view some post, the post views counts +1.

What i want...
is it possible to count the post views +1 only when the visitros view that post for 10 or more seconds?
if its possible, how can i do that?

Sorry for my bad english.
Your english is much better than I could write in your language, I am sure. No problem.

Someone else may have a better answer for you, but my first thought would be to have a Javascript function that starts a timer in the <body onload= event, and after 10 seconds it sends an asynchronous HTTP request, like Ajax, that calls a PHP script that updates the number of views in your database. Maybe there's a simpler way.
User avatar
lonelywolf
Forum Commoner
Posts: 28
Joined: Tue Jun 10, 2008 6:15 am

Re: Is this possible?

Post by lonelywolf »

You can create a session variable to check whether increases post view or not.

Code: Select all

$newsExpire = 10;   //unit: second
if(!isset($_SESSION['newsViewSpam'])
    || (time() - $_SESSION['newsViewSpam'] > $newsExpire))
{
    //query DB to increase your post view
    //...
 
    //assign new timer
    $_SESSION['newsViewSpam'] = time();
}
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Is this possible?

Post by WebbieDave »

lonelywolf wrote:You can create a session variable to check whether increases post view or not.
Employing a session will not afford him the ability to discern how may seconds a visitor was on particular page. The Ajax suggestion would work, though.

For non-Ajax platforms, perhaps a hidden iframe that loads the logging page after 10 seconds.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Is this possible?

Post by Ollie Saunders »

My initial suggestion would have been the same as califdon's.

But I think you could also use a session. It would only work when moving to another page within your site but wouldn't require JavaScript. Then you could augment that behaviour for users with JavaScript available to work even when leaving your site by sending an AJAX request on page unload and computing the difference from the last session time. Does that make sense?
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Is this possible?

Post by WebbieDave »

ole wrote:It would only work when moving to another page within your site but wouldn't require JavaScript.
Right. I guess it comes to down to the accuracy required by the OP.
Post Reply