Quick PHP Question

Discussion in 'OT Technology' started by o2, Sep 16, 2006.

  1. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    Im rather new to php, and Im coding myself a CMS. I ran into a small problem with the item hit counter (shows how many times a video has been viewed).
    I simply have a function add_hit($type, $id) which adds 1 to the "hits" field for that specific item ID. SImple... the problem is that it executes upon when a user comments on a video, or refreshes the pages.
    How can I only count the initial page load as a hit, and ignore the refreshes, etc?
    It has to workt he same way for logged in and logged out users.
     
  2. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Just a question: Are you reinventing a CMS because you like to program and want to, or because you think you have to?
     
  3. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    Im doing it for practise, and if its good enough, to replace the current script I use on all of my sites.
    By CMS... I dont mean stuff like phpnuke.... this is content specific (for video, and link dump sites). Its pretty similar to what I use on www.grayvee.com (NSFW)
     
  4. piratepenguin

    piratepenguin New Member

    Joined:
    Jun 18, 2006
    Messages:
    1,067
    Likes Received:
    0
    Location:
    Ireland
    Store their IP and only add it if its not already in there?
     
  5. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    Thats gotta own the server pretty fast.... since my site gets over 70k uniques per day. Thats gonna be over 100,000 records per day that would be added to the DB.
     
    Last edited: Sep 16, 2006
  6. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    Use sessions/cookies to determine whether or not you've already logged something as a view -- no extra server load.

    Code:
    function add_hit($type, $id) {
    
    if($_SESSION['hitflag']==1) return;
    $_SESSION['hitflag'] = 1;
    
    // your other code here
    
    }
    
    Make sure you session_start(); before accessing/writing to the session variables, if you haven't used sessions before then read up on them -- my code is incomplete.
     
  7. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    Eh, that'd only allow each person to log a single hit for a single video (if they hit others it wouldn't log it). So you'd want to change it to something like:

    if(strstr($_SESSION['hitflag'],",$id")) return;
    $_SESSION['hitflag'] .= ",$id";
     
  8. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Things like this make me glad I chose a framework, and a language that has frameworks.

    /perl troll
     
  9. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    I tried sessions myself... but it didnt work...

    PHP:
    if (!$_SESSION["viewed"] == 1) {
    add_hit($id);
    $_SESSION["viewed"] = 1;
    }
    But of course if you view 1 video.... it will not add hits to any other video, because "viewed" session var is already set to 1. How can I make it have an id variable inside session variable? So for a video with an id of 546 it would be $_SESSION["viewed546"].

    This is the script Im working on btw: http://www.planetboredom.net/cms_dev/index.php

    My first php project :)
     
  10. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    $_SESSION['viewed' . $id] or $_SESSION["viewed$id"]
     
  11. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    Creating 10s of thousands of session variables per day wont affect performance?
     
  12. m2e

    m2e OT Supporter

    Joined:
    Aug 9, 2004
    Messages:
    48,121
    Likes Received:
    56
    Location:
    US
    hey o2, how much revenue a month do you make off your website by ad's alone? :o
     
  13. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    Off grayvee? Not that much, I didn't go all out with the ads (yet). I wanna reach 150k/day and then whore the ads.

    Its about $50/day or so.

    My SFW sites make more (with less traffic).
     
  14. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Never worry about scalability until you actually have a problem. People over worry about this stuff.
     
  15. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    I think its easier to prevent a problem then spend days tearing ur hair out and optimizing ur code, because ur box keeps swapping with 4GB of ram, and running 30+ load on dual opterons.



    Thats just me...
     
  16. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Unless you do something incredibly stupid, that is not an issue. And its not an issue here. But your statement illustrates my point: people waste WAY too much time thinking they've got amazon's scalability problem.
     
  17. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    I dunno... 100k/day (and growing at about 15% per week) is pretty server intensive. it might not matter now, but when Im doing 400k/day, it will.
     
  18. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    My comma delimit should be fine:

    if(strstr($_SESSION['hitflag'],",$id")) return;
    $_SESSION['hitflag'] .= ",$id";

    That will create one session variable for each user while the new variable per id will create multiples. Sessions will be MUCH faster than storing IPs in a database or file.
     
  19. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    Ok thanks, I'll give it a try!
     
  20. m2e

    m2e OT Supporter

    Joined:
    Aug 9, 2004
    Messages:
    48,121
    Likes Received:
    56
    Location:
    US
    nice :cool:
     
  21. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    Hmm make that $100/day now.

    :cool:

    Cant wait to get my new woodcrest powered box! The poor P4 is struggling....
     
  22. Xyroquell

    Xyroquell New Member

    Joined:
    Sep 16, 2006
    Messages:
    51
    Likes Received:
    0
    I use the database to store this information. Of course it's slower than session-variables, but it's more flexible and easier to accumulate.

    When a user enters the site he'll get a session-id (I'm not using IP-addresses as that causes trouble with load-balanced proxies). Each page that gets accessed by that user will generate an entry in the database. On this point I don't care on duplicate entries.

    The table has the following info:
    - timestamp
    - session-id
    - page-id
    - user-id (once a user logs into the website, all the user-id fields on the entries with the users session-id get filled with the user-id. Even after a user logs out, the newly visited pages still obtain this id.)

    At night, this table is exported to disk and all exported entries are deleted. Because of this, the table does not have an indes or primary key. The exported table is then moved to another machine where it gets imported. On this second machine the tablecontents will be stored for 4 weeks.

    Each night a script runs on the second machine that starts accumilating data. The imported contents are converted to another table:
    - period (1-31 for days, 1 - 52 for weeks, 1-12 for months and 4 digits for the years [range from 0 - 9999])
    - period-type (defines the information in the period field (1 = daynumber 2 = weeknumber, 3 = monthnumber, 4 = year)
    - user-id (= 0 for anonymous)
    - page-id
    - visits (counts the number of visits)
    - unique_visits (the number of visits with different session-id, or once accumulated, the sum of the accumulated entries)
    On the end of each week the information with period-type 1 will get accumulated to weeks. However, the original entries remain in the DB. On the end of each month, the information with period-type 1 gets accumulated to months. Yes, I don't convert from weeks to months! Instead I accumulate the daily information twice.

    After three months the daily information gets exported, zipped and moved to the archives. When the db gets to much entries, this process can be started manually, for the price of loosing daily statistics.

    In the end I have a nice view of which page is requested, when and by who. For the first three months I can have daily information and after that I get information per week or month.

    Eventually you can accumulate to years if you have information over multiple years and want to make room. And if you really want to know how many unique visitors are visiting your site, you can set a tracking-cookie and log that.

    BTW: I strongly advice you to keep an eye on the size of your logging-table. If it gets to large during the day additional exports may be required. But those exports take up resources so if your website is too busy, consider placing the user-id and/or session-id in the URL and don't use the database for logging anymore. Take the webservers access-log instead. It is a bit harder to import, but all the information you need is there!
     
  23. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
    Wow, that sounds like a huge overkill.
     
  24. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    In contrast... I just configured Catalyst authentication, and it took all of 10 minutes and I never have to actually think about the details again. Sessions and authentication just plain work. PHP guys... how can you live without frameworks? Whats it like reinventing the wheel all day? Couldn't you be more productive spending your time on the aspects of your application that are novel, instead of the ones that every application has... like sessions and authentication?
     
  25. Xyroquell

    Xyroquell New Member

    Joined:
    Sep 16, 2006
    Messages:
    51
    Likes Received:
    0
    Depends on what customer wants to know. In this case the customer was curious how often certain product-pages were requested. Should he promote more? (Yes, the referrers were also stored). Should he move information to the product main-page instead of hiding behind a link? Is a returning customer possibly interested in other features or add-ons for the product?

    In short, everything on the website was checked, logged, accumulated and reported to the customer's marketing department and management. And it didn't really matter if customer A requested FAQ-X a thousand times, but if 10 customers check that FAQ frequently, they want to include the information in the product-manual or in the next product-design.

    And a database is excellent for storing this kind of information because you can run the most idiot queries on the data.
     

Share This Page