Youtube Hits Counter

Discussion in 'PHP' started by oo7ml, Jun 15, 2012.

  1. #1
    Hi, i am in the middle of developing a website, and i would like to add a function to my site whereby a member can see how many times their profile page has been viewed, similar to the way YouTube displays the amount of hits a video has.

    I have a few questions which i hope you can help me with:

    A - who does the YouTube counter work OR what is the best way to build these counters... are they (1) - IP based, (2) - Cookie based OR (3) - another method

    B - does the page make a database update each time a unique user lands on a video page

    C - how often is the number updated... for instance, if i view a video on youtube on several different devices all connected to different networks, the number does not update immediately... so how does it work

    Thanks in advance to anyone who can offer any help here ;)
     
    oo7ml, Jun 15, 2012 IP
  2. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #2
    You might as well forget about how YouTube handles video view counts since that is NOT what you are looking to do.

    If you are just keeping track of profile views for your own members amusement and it doesn't play a critical role in any algorithms your web site may have you should count every literal load of the profile.

    I think your direction is skewed. You are asking questions about how YouTube keeps track of how many times a VIDEO is viewed yet you clearly stated you are looking to keep track of a view count of a web page.
     
    NetStar, Jun 15, 2012 IP
  3. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Hi, thanks for your reply... i understand what you are saying, apologies for my poor analogy :( however i don't think it is right to just update $profile_views by +1 every time a users profile page is loaded as the hits will be inaccurate... as i don't want the hits to be +50 if a user simply refreshes a users profile by pressing refresh constantly... what approach would you consider then, thanks again for your reply...
     
    oo7ml, Jun 15, 2012 IP
  4. JasonPage

    JasonPage Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    YouTube's counter is a little more complex than simply IP or cookie based. YouTube frequently freezes the viewcount for hours on end while it investigates sources of traffic (presumably manually) to check for people trying to cheat the system, this is particularly important to them due to the nature of users earning revenue through their videos. You notice it most on videos with low viewcounts that get the reddit bump, they often end up with more likes on the video than views.
     
    JasonPage, Jun 15, 2012 IP
  5. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #5
    Yeah, i think youtube was a poor analogy :(

    I just need to record page views but don't want refreshes to be included etc...
     
    oo7ml, Jun 15, 2012 IP
  6. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #6
    First off... a profile view technically is a page view or impression regardless how many times a particular visitor views that profile. Obviously you are keeping track of views and not unique visitors correct? Again...IF the view count is ONLY for the members amusement and the accuracy is irrelevant to any other features on your web site it is not worth going through the extra steps to prevent "cheating" since there is no benefit to cheat other than increasing your view count for your own amusement.

    IF view count accuracy is vital to your web site than you have no choice but to come up with some sort of serverside and clientside method to prevent cheating and revisits. But it will never be accurate.
     
    NetStar, Jun 15, 2012 IP
  7. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #7
    Out of 10,000 single page views you may have a couple refreshes. It's not a big deal.
     
    NetStar, Jun 15, 2012 IP
  8. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #8
    Use a session variable to increment the page count for a single session only once, no matter how many times the user returns to the page.
     
    Rukbat, Jun 15, 2012 IP
  9. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #9
    Hi, first off i do need to explain that this figure is important and does affect other elements of the site so i would like to make it as secure as possible...

    Hi, thanks for the reply... however, how would i control when their session would be open to constitute as a another hit, would it be possible to reset their session variable every 5 minutes... but that would probably effect other page hits then :(

    I remember Bebo had a profile counter too... and i figured out, if you landed on a profile page, that would constitute as +1 hit... however if you refreshed the profile page, nothing would happen, however if you clicked out into some other page on the site and then went back into the profile page, another +1 would count... i think this would be ideal, but not sure how to go about it... and the figures took a few hours to update also on Bebo...
     
    oo7ml, Jun 17, 2012 IP
  10. davidson_11

    davidson_11 Active Member

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    80
    #10
    As Rukbat suggested, I advise you use a session for the counter. In regards to expiring the session, here is an example:

    if (!isset($_SESSION['counter'])) {
         $_SESSION['counter'] = time();
    
         // Code to increase counter by one (e.g. code to update counter in database)
    
    } else if (time() - $_SESSION['counter'] > 300) { //300 Seconds, which is 5 minutes.
    
         //Update counter after 5 minutes
    
         session_unset($_SESSION['counter']); // Destory time stamp
         $_SESSION['counter'] = time(); // reset time stamp
    
         // Code to increase counter by one (e.g. code to update counter in database)
    	
    }
    Code (markup):
     
    davidson_11, Jun 17, 2012 IP
  11. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #11
    Sessions are about as secure as any other part of web technology. Absolutely secure? No, there's no such thing. But Davidson's code will do what you want.
     
    Rukbat, Jun 17, 2012 IP
  12. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #12
    Ok thanks guys, but how does the counter work for a specific page or is that session counter created for each profile page you visit, thanks for your help, much appreciated...
     
    oo7ml, Jun 17, 2012 IP
  13. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #13
    The session variable is created for the user's session. The counter is your choice - one for each page or one for the site. It depends on what you want to count.
     
    Rukbat, Jun 17, 2012 IP
  14. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #14
    Ok cool, thanks for clearing this up ;)
     
    oo7ml, Jun 17, 2012 IP
  15. rox005

    rox005 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I will Give you Cheap Youtube views upto 1000k =D

    If anyone want it Then contact me on skype

    skype = google_sale
     
    rox005, Jul 4, 2012 IP