SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 26
  1. #1
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    count # times link is clicked (in db)

    Hello,
    I'm trying to make some changements to this article cms i have, and one of these changements would be to count how many time an article is viewed.
    I don't want a hit every time the article page is loaded, but every time the article link is clicked.
    Can someone tell me how to do this? I have no idea how to start on it.
    Thanks!

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sanzo
    I don't want a hit every time the article page is loaded, but every time the article link is clicked
    Don't you have to click on the article link to load the article page? Can you explain your problem more clearly?

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Swindon, UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kailash Badu View Post
    Don't you have to click on the article link to load the article page? Can you explain your problem more clearly?
    No, because then a refresh would add to the 'click count'.

    The way I see it, there are two ways to do it:

    1. Use Javascript and an IFRAME to load a hidden counter increment, and then get it to redirect to the page you require
    2. Pass the page of the article you wish to visit to a seperate page that counts the click, updates the database, and then redirects you to the page you require


    The advantage of the first one is that it would be seamless for the end user - they wouldn't be redirected to two pages, it would all be done in a hidden iframe, and then just redirected to the article - However, for this, the user must have javascript and frames enabled, which isn't ideal

    The second idea removes these requirements, requiring just a straight link to a tracker page, but you will esentially be sending the user to a dummy page, and the redirecting them to the article from that page

    Just my thoughts - if you want some more help on how to actually achieve these let me know

  4. #4
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well i would like to do it with the clicking way.
    Isn't there a way around it with a cookie or session or something?
    If not, how do you do it so it also clicks on page refresh?


    @jazzer01, thanks for ur suggestions, but i'd prefere not using frames. And the 2nd way is kind of long.

    Thanks!

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Swindon, UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To do it just on page load - so that it also increases on refresh, all you would do is write a routine at the top of the page that increased the counter by 1:

    PHP Code:
    <?
    $query 
    "UPDATE `tracker` SET `count` = `count`+1 where `article` = '$article'";
    mysql_query($query);
    ?>
    Where $article is however you want to identify the individual articel - without knowing your DB structure, I don;t know exactly what fields you have, but thats a general idea of how to go about it.

    I'm still trying to come up with a click way - however, cookies are not great, as they are easily rejected - and wouldn't differentiate between a click and a refresh anyway...

  6. #6
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do you want to count the 'refreash' as a page view or not?

  7. #7
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @ Kailash Badu: I'd prefere not, but it looks kindof hard to do without it ...
    @jazzer01: With a temporary url, that redirects after saving the hit i'm not sure if it would be good for seo (what url's do se's index?)
    I hope you come up with something

    Thanks!

  8. #8
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you use a normal view counter. Every time a page is loaded, increment the value in the database. In the 'Article' table, just include a column called 'view';

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Swindon, UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sanzo View Post
    @ Kailash Badu: I'd prefere not, but it looks kindof hard to do without it ...
    @jazzer01: With a temporary url, that redirects after saving the hit i'm not sure if it would be good for seo (what url's do se's index?)
    I hope you come up with something

    Thanks!
    Unfortunatly you are right, it would be kinda awkward for SEOs - they would see the redirect link and page, although you could include a keyword rich link on that tracker page, linking to the article in question, so that the spiders would follow through to the actual article (and so possible index the article).

    The other option I did think of is to open a tracker page in a new window that closes as soon as the update is performed. However, with many users blocking popups, this is not a particullaly good solution.

    I'm just heading off to a meeting now, so if I have a brainwave, I'll post when i get back

  10. #10
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about this;
    in the page where the article is viewed, only counting a view, if a referral is set?
    I supose there isn't a referral set when refreshing a page is there?

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Swindon, UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To be completely honest, before I did a test, I didn't know - however, on a page refresh, the HTTP_REFERER is still there, so unfortunatly that will not work

  12. #12
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh that sucks
    i just tryed, and unset didn't work nether, or i did someting wrong, or it's just impossible

  13. #13
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Swindon, UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There must be a way - there is always a way!!

    hm....what about using a XMLHttpRequest?? Not sure why I didn't think about it before....i can't see why it wouldn't work - except it requires javascript, so people with javascript disabled would have problems....

  14. #14
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does the program keep track of whether the same user clicks on the article or not?

    If it does, set a cookie in the top of the viewed page, so that if they refresh or, click it again it wont increment your counter

    If not, i think the best way would be to have a redirect page. There is no sense messing around with ajax, and all this other stuff, when it could be as simple as making a redirect page.

  15. #15
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Swindon, UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did recommend a redirect page at the beginning, but sanzo really didn't seem to like the idea - hence the wierd and wacky attempts to come up with a solution! - although his main issues was with SEO issues, so perhaps this is still a workable solution? sanzo?

  16. #16
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    if(!$_SESSION['click']) {
        
    $sql mysql_query("update table set counter = counter+1 where article='whatever'");
        
    $_SESSION['click'] = 1;

    whats wrong with something like that?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  17. #17
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Swindon, UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hm...why didn't we think of that?

    Only issue is that if they clicked the article again, then it wouldnt be counted as a click - but then again, if they have visited it once, why would they need to re-click?

    I feel really stupid now..... :P

  18. #18
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can someone explain me how that works please?
    I don't really understand.
    And also, when does this expire or something, so the click is counted again?

    Thanks alot guys!

  19. #19
    SitePoint Zealot tconley79's Avatar
    Join Date
    Feb 2004
    Location
    Miami
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how about making the link a submit button that passes a hidden variable...

  20. #20
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't be SEO friendl neether i think.

  21. #21
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by sanzo View Post
    Can someone explain me how that works please?
    I don't really understand.
    And also, when does this expire or something, so the click is counted again?

    Thanks alot guys!
    Hi sanzo,

    When the page is loaded, the script checks for the $_SESSION variable. If it's not there it sets it to 1. If the page is then reloaded by the user the script can see the variable and so wont add to the counter.

    It the user goes off around the site and then comes back to the same page, the variable is still there and again wont add to the counter.

    It remains until the user closes their browser window or until you unset() it.

    For reference:
    http://www.php.net/manual/en/function.session-start.php
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  22. #22
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So if i understand this, this should come on the page where the article is shown.
    And how should the link look like in the first page then?
    Thanks alot!

  23. #23
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Yes, this appears at the top of the page where the article is displayed. You will need session_start() in there as well.

    so your code would be:
    PHP Code:
    session_start();
    if(!
    $_SESSION['click']) { 
        
    $sql mysql_query("update table set counter = counter+1 where article='whatever'"); 
        
    $_SESSION['click'] = 1


    Your links wont change from what they are now
    Obviously you will need to change the query line though to reflect your table!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  24. #24
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Swindon, UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just thought - once the session is set, and you visit one page, any other articles you click on won't be counted..... or am i missing something?

  25. #25
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    nope, you arent missing anything
    PHP Code:
    session_start(); 
    $article_id $_GET['article_id'];
    if(!
    $_SESSION['click'][$article_id]) { 
        
    $sql mysql_query("update table set counter = counter+1 where article='whatever'"); 
        
    $_SESSION['click'][$article_id] = 1

    quick amendment to the script to add the article_id into the variable

    Good spot jazzer
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •