SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Story count - some problems?

    Hello,

    I'd like some advice with a story counter i've created which works on a users IP address and user agent

    Basically the counter is added to if:

    1) $_SERVER['HTTP_USER_AGENT'] is not empty. If it is then I don't add to the story count (as normal browsers idenitify themselves)

    The problem being with this is that this is not fullproof. Is the best way of this maybe creating an array of banned user agents in an array and checking that array with the following in it:

    http://www.sitepoint.com/forums/show...78&postcount=3

    Or is there a better way of doing this?

    2) The IP address of the user (which is checked in another table) already exists then the counter is not added to

    Big floor in this is that say a user visits a story on Monday then visits again on Tuesday the counter will not increment as the IP address exists. But then again I don't want the user sitting there hitting refresh 15 times to add to the count (Which is why I record the IP)

    So are there any better ways of doing these two things?

    Thank you

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    You could add a date/time field to the database.

    But be aware that an IP address is not neccessarily unique. Multiple users could have the same IP number. And user-agent can be omtted or spoofed.

    Still, it should at least give you a rough idea of visit stats.

  3. #3
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I thought about adding a date/time field, but surely that would still cause a problem with refreshing ie say I recorded the time as:

    2009-12-04 19:22:04

    Then the user hit refresh a second later

    2009-12-04 19:22:05

    The user would be recorded twice?

    Thanks

    Chris

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Use the DATE type: http://dev.mysql.com/doc/refman/5.1/en/datetime.html
    It'll contain only the date.

  5. #5
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7724 View Post
    Thanks. I thought about adding a date/time field, but surely that would still cause a problem with refreshing ie say I recorded the time as:

    2009-12-04 19:22:04

    Then the user hit refresh a second later

    2009-12-04 19:22:05

    The user would be recorded twice?

    Thanks

    Chris
    You can use redirect header('Location: ' . $mypage); after succesful add to prevent the refreshing problem.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    I was thinking that you could use date/time and test to see if an arbitrary amount of time had elapsed. Not simply recording it. Of course how much time? 10 seconds, 1 minute, 5 minutes, half an hour, etc? No choice would be fool-proof, but you might be able to get something close.

  7. #7
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your thoughts guys. I know this isn't the answer (and google analytics is!) but I'd like some basic stats.

    I like TenDollas idea of

    PHP Code:
    ('Location: ' $mypage); 
    But if the page i'm is is say page.php?id=1

    and the script runs i'd have to send the user back to the same page page.php?id=1 as I use GET to get the id of my story and the details of that story from the database. Or could this be achieved differently?

  8. #8
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure if I am following you but you can redirect to any page you want. Say for example you come to page 'page.php?id=1';

    PHP Code:
    // When arriving to the page store the id from GET.
    $id $_GET['id'];
    // Then add things to db... and if it was successful redirect to same page
    header('Location: page.php?id=' $id); 

  9. #9
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but what i'm getting at is when you are re-directed the script would run again as you are effectively re-directing to the same page??


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
  •