The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 11 of 11

Thread: PHP text file hit counter

  1. #1

    PHP text file hit counter

    As part of a game I'm developing I would like to be able to track the number of times the game is played. If a player reaches the end of the game they can submit their details and I have the neccessary files in place to track and display this.

    However, I would also like to be able to count each and every player who starts a game without neccessarily finishing it.

    To this end I have created a simple script that performs a simple counting calculation. I didn't want to invlove a database for this part of the project as we are anticipating (or at least hoping) to have a fair number of visitors and I don't want to be making database calls at the start of every game.

    So, my counter as it stands is this:

    PHP Code:
    $fp fopen("playCount.txt""w");
    playCount.txt is simply just a text file that contains the number 0 to start with.

    This works perfectly and does everything I need it to, but I've suddenly realised the implications of opening a file, reading its contents, and then ammending the file, if many users are doing this at the same time. Are there any troublesome effects this could cause? I'm not particularly worried about extreme accuracy of this counter, but would like to know if there are any more serious problems it could cause?

  2. #2
    well, yes, it will just not work sometimes if many people are doing it at once, and at the end of the day, it's harder and less efficient to fwrite than update a MySQL field

    member #1 of the "XML-is-a-crutch-to-make-people-feel-elite-club"
    K-Emmys-06: Best Quote K-Emmys-06: Flash Genious

  3. #3
    So you think it's better to just increment a variable that sits in a MySQL table? I just assumed that opening a connection, changing the value and then closing the connection was a bit more server intensive than writing to a file?

  4. #4
    no, you should not be updating a counter in MySQL. it's fastest (and most useful) to write an INSERT. that makes it easy to grab all sorts of good info like:
    - datetime played
    - user ip
    - user browser
    - etc.

    and it serves as a counter.

  5. #5
    So in your opinion (which I trust), the most efficient way is simply to insert a new row into a table for every user that start's a game?

    I'm already logging date/browser/ip for those that finish the game.

  6. #6
    yes. i would do all of that logging upfront. basically, i would do this:
    - user starts game
    -> log game started (date, browser, ip, etc)
    => return game_id (auto_increment)
    - user finishes game
    -> log game finished (by game_id)

    for a quick and dirty solution, my db structure would probably be something like this:
    game_id (PK, auto_increment)
    i would make the is_finished column a binary field. default 0. update datetime_end and is_finished when the user finishes.

    but if you wanted it smoother, you could create a Browsers and Users table.

  7. #7
    Well honestly, I don't really want to log all that information for every single play of the game. Basically, there's going to be a prize awarded for the highest score, and you can only submit your score once you've finsihed the game. So that is where I'm logging the details, which happens through a flash interface via amfphp to the database. I think it's going to be a bit of overkill if I log these details every time someone starts of a game.

    So, if i'm literally just inserting 1 row, with 1 value, into a table with only 1 field, is this still the most efficient way?

    INSERT INTO `playCounter` (`wasPlayed`) VALUES ('1');

  8. #8
    my opinion is that if the data's there and it's low hanging fruit, you should grab it. my suggestion would provide all sorts of useful stats like:
    - what percentage of people that start the game actually submit scores?
    - how long are people usually playing the game before submitting a score?
    - what times of day are people playing the game? what days of week?
    - are people with certain browsers starting a game and not finishing it, possibly because of a bug?
    - what percentage of people are return players?
    - etc, etc.

    at best, yours can grab a warped version of some of the aforementioned stats, but not all.

    my INSERT would run just as fast as yours, but i would run more often. but i don't think it would result in any visible lag.

    if you're still going to use your method, you might as well write an UPDATE like this:
    UPDATE playCounter SET wasPlayed = wasPlayed+1
    then just have one row in the table and one column (wasPlayed). again, i don't see any real benefit from this approach.

  9. #9
    I really appreciate all this help, but:

    If im already logging the number of people who finish, and I'm implementing a counter to see the total number of starters, I can already see the percentage of people who actually finish.

    You can only submit a score if you finish the game. The game has a timer, with it's final value being submitted upon submitting your score; so I can already see exactly how long people are playing for.

    I'm already logging date/time of play, upon submitting score. Granted I can't see this information for those that don't finish, but I'm not really that interested in them.

    While it's true that seeing if people with certain browsers are starting and not finishing could flag certain bugs, I'm pretty sure I've done / am doing sufficient testing to prevent this.

    lastly, I've been working on this project single handidly for 3 months - concept, design, actionscript, graphic design, database design, load testing, level design, php, backend design, amfphp service etc etc; and while some of these features could be beneficial (% return players is a good one), I now have approximately 3 weeks to finish, so they may just have to stay on the wish list unless I really get my arse into gear.

    So, at the moment, assuming Im going down the route of:

    Start game triggers simple counter
    Finish game submits extended data to database


    UPDATE playCounter SET wasPlayed = wasPlayed+1

    the most efficient way of simply counting versus a text file system similar to the one described earlier?

    Damn I type too much.

    Thanks again.

  10. #10
    i'm not certain about efficiency, but overall i think the UPDATE is a better option than using a text file. you would need to test each for speed to really gauge efficiency.

  11. #11
    What happens when two users try to play the game at the same time? One user has the file open for writing so the second cannot. So the only real problem is what will happen to you're php code does it still return a valid html or will there be fatal errors. So this is the only interresting fact of the mather. If there are no fatal errors than you could lose a few hits.
    It's true that database operations are faster then file operations but it's only expressed in milliseconds. So lets say 1000 users hit you're game in one our. You would lose ,maybe a second!!!!

    Oh if you want to try the above. try to open a file that you already opened and see what happens...

    Hope it will work

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012