What is the best way to record unique views/clicks?

Discussion in 'PHP' started by zerxer, Jan 7, 2009.

  1. #1
    I know most of you will say the obvious and that's to record IP addresses but I am not sure the best way to go about it. Would you store every IP in the database as well as a list in one column of all the pages they've viewed and just keeping adding onto that? I know that's probably the only way, seems like the table would get huge. Would you then prune the table every day or week or month or year or what? What about shared IPs such as a college dorm? Would only get one record out of all of them combined. Can't really do a cookie since someone could just keep clearing their cookies.

    I am about to start on a website that relies on recording views/unique views so this is why I want to make sure I'm doing it the most reliable way possible.
     
    zerxer, Jan 7, 2009 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    I'd recommend if you simply want to record how many unique visitors, and not what time period they visited at, a single column database would do, ensure it is set to unique and indexed as well as possible. (Use 'int' for the column type as it would need to hold numbers as large as 0xFC05FC01. (Keep them in IPv4 long form.)

    Simply perform a lookup on the table, if it the IP doesn't already exist, do nothing - else add it into the table.

    If you want to record the time and the number of visits, the same as above but with a timestamp (and probably an identifier column) would suffice, simply add the new row each time.
     
    Danltn, Jan 7, 2009 IP
    zerxer likes this.
  3. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well, this isn't just for a total unique views for entire site, this is on a per-item basis. Alright, let me give you an example here. dragcave.net, users are given an egg, they must get views/unique views/clicks on it to have it grow and hatch. It records unique views for every single egg/hatchling/dragon on that website, not just for the global website itself. No, I am not cloning this site, it's just a great example.
     
    zerxer, Jan 7, 2009 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Assuming each egg or whatever has a unique ID, a table with ID, IP, Time, Egg_ID should suffice, where Egg_ID is an actual Egg_ID. (uses the MySQL 'References' feature for this.)

    Dan.
     
    Danltn, Jan 7, 2009 IP
  5. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hmm, and you don't think that'd become too big of a table? Obviously can then delete every row that references an item that no longer exists so that can sorta help control the size. Another thing I can't decide on is if I want to record a unique view for a person every 24 hours so once every day at midnight, the table will be pruned, and users viewing a page will increase the unique views more. While I'm not cloning that website, I'm making something based around that general idea.

    I'll also check out MySQL references, if it's actually something special. I know how to work with SQL enough to do basic queries and some join statements. What I would've done is just stored the item's ID in an integer column but I'm not sure if that's what you meant by an actual reference of it.
     
    zerxer, Jan 7, 2009 IP