1

The hosting package im using wont allow sql and will have to pay more monthly if i want sql and its on a shared server so i cant have a client server set up for my app.

Im making a leaderboard for my first ever app.

so iv decided to do everything using php. the app will launch a link in the browser which will look something like this:

....../myAppLeaderBoard.php?opt=submit&?id=5465&name?='myName'&dat=DKGHKDHGKHDKGHSAJDHKJAHGJKHDFGHKJDFHGLKHDFGJHSDJLFGHJKSDHFGKJDSHFKGJHSLKDFHGLSJDHFGLJSHDFGJHSLDFJHGLSDJHFGLSDHJFGLSHDFGHG

All those alphabets is because i plan on using my own encrypting technique to prevent a user from cheating and giving themselves a highscore.

when its submitted it will read everything from a textfile into an array it will check if the user exists and if they do it will change that record and if they dnt exist it will add a new record,then it will write everything from the array back to the textfile.

Then it will display a success message and show the user on the leaderboard,Now what will happen if say 100000 users each simultaneously submit there scores.

If it reads and writes one record at a time there wont be a problem but if it does this simultaneously then some records might be deleted by a simultaneous write.

So is it done simultaneously or one at a time?

Feel free to give suggestions for a better way to do this.

5
  • why can't you buy a better hosting plan if your app is going to be used by 100000 users simultaneously. :P
    – AeJey
    Jun 6, 2014 at 11:15
  • I suggest you learn about building any kind of web-app before focusing on the kind that can have 100,000 simultaneous visitors. Jun 6, 2014 at 11:16
  • If by "encrypting" you actually mean "obfuscating"... that won't work :-) It can be done, but you don't want to do it. (e.g: Homomorphic encryption, or perhaps Proof-of-Work). Jun 6, 2014 at 11:18
  • You will need to lock the file when editing, to make sure it's not being overwritten. However, as suggested this is a very bad solution.
    – Colandus
    Jun 6, 2014 at 11:19
  • I don't have 100000 users at the moment i was using that as an example. At the moment I have 50+ users and it increases everyday. Also at the moment i cant afford i better hosting plan. earnings from app only come a few months from now. but in time i will upgrade so for now I'll be using sqlite. thanks for all the help everyone
    – CodeWorm
    Jun 9, 2014 at 17:36

2 Answers 2

3

File access is simultaneous but you can use flock to block other handles from accessing the file while you perform your read/write operations. It sounds like the best solution to your problem would be to use PDO and SQLite (if available). This way you have the database handle all locking for you but do not need a dedicated database server. SQLite is entirely file based.

If SQLite is not available, you'll want to make use of flock's LOCK_EX operation, this will only allow one write stream to access the file at a time, e.g.

// create the file handle
$hnd = fopen($destpath, 'ab+');

if ($isReadOperation) {
    // shared lock - simultaneous reads can happen at one time
    flock($hnd, LOCK_SH);

    // perform your read operation
} else {
    // exclusive lock - only one write can occur at a time after all shared locks are released
    flock($hnd, LOCK_EX);

    // perform your read/write operation
}

// release the lock
flock($hnd, LOCK_UN);

// release the file handle
fclose($hnd);
0
1

File access (and this applies to sqlite databases as well, as they're file-based) is, unfortunately, not supposed to handle many simultaneous read & write operations. Therefore, you will run into problems with that.

I'm afraid your only sensible option is buying a hosting plan that offers a real database, e.g. MySQL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.