SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Writing High Scores to a File

    Hi,

    I'm developing a flash game and there is a high scores table in it I'm working on.

    I'd like to use a simple text file to read/write high scores with PHP.

    The potential problem is that if there occurs multiple request to write to the file, the file/data might be corrupted. Is that right?

    Should I lock the file while writing? If so what happens another user submits a score while the file is locked? How to solve this?

    I hope the problem is clear enough.

    I'll switch to a database if the problem I tried to explain really exists and the solution is complicated.

    Thanks.
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, you can lock the file, look at this function
    http://us3.php.net/file_put_contents
    as a third argument put LOCK_EX

    But I think the plain text file may not be the best way to store data. I mean, if all you have to store is just one line with a value of high score, then sure, a text file is fine, but if you also want to store username => score pairs then you should consider using an xml file.

  3. #3
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I agree XML would be more compatible and expandable but user/score pairs are pretty easy to handle with TXT as well (plus shorter data).

    Anyway back to my actual problem:

    Suppose I locked the file while someone is submitting score. What happens another user submits at the same time? I guess it will fail isn't it? What is your solution to this issue?

    As far as I know, multiple simultaneous writes/updates is not an issue in SQL databases. I think it would be hard to queue the requests if I go through the text/XML file path.
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  4. #4
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, if the file is locked, php will wait untill lock is removed. As soon as lock is removed it will obtain its own lock and write to file

    The queue is handled by file system, you don't need to worry about it, just like with database. It's actually may be more problematic with some databases because unless the row level locking is available, the entire table is locked when a process writes data to a table.

    If you use mysql with default table format then row level locking is not available.

  5. #5
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's good news thank you. I was hoping for that but didn't know for sure.
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter


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
  •