473,503 Members | 756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Saving High Scores to A Text File - Multiple Requests

88 New Member
Hi,

I've built a Flash game and using PHP to save scores to a text file. Everything works fine so far.

My potential problem is that I'm not sure what happens when there are multiple requests to write to the file simultaneously. The file might be corrupted, or the request(s) might fail?

I'm not good at file system functions so I need your help. This is the basic structure of my script:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.     $handle=fopen("theScores.txt", "r+");
  3.     $data=fread($handle, filesize("theScores.txt"));
  4.  
  5.         //prepare the output here
  6.  
  7.     ftruncate($handle, 0);
  8.     fseek ($handle, 0);
  9.     fwrite($handle, $output);
  10.     fclose($handle);
  11. ?>
  12.  
Now, I know about the flock function. Is it the right solution to queue the requests and prevent data loss/corruption.

How should I proceed?

Thanks.
Jul 30 '09 #1
3 3514
dlite922
1,584 Recognized Expert Top Contributor
Yes, if PHP doesn't get a lock with flock() it waits until it does. (you can test this with a short script) unless your provide LOCK_EX | LOCK_NB. But you don't want this, unless writing to the file takes a long time and you don't want the user to wait.

How are you planning on doing the queues?


Dan
Jul 30 '09 #2
serdar
88 New Member
Thanks for the answer.

Still not clear what happens when a user tries to save a score while another is already submitting score.

Suppose I locked the file with flock while processing the first request.

Does this mean the second user will simply wait (most probably for a very short time in my case) and this request will be processed successfully after the first?

Or does it mean, the second request will be ignored or worse cause data loss/corruption?

This is what I called a queue in my first post. I mean, do I need to take care of anything else to maintain the queue the way I described?
Jul 30 '09 #3
serdar
88 New Member
I've made a few tests; sending multiple requests to read/write the file and it worked perfectly.

As I'm told here and at some other forum, the queue is handled by the server and you don't need anything else than using flock().

Thanks.
Aug 1 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
3646
by: Irmen de Jong | last post by:
Hi, Things like Twisted, medusa, etc.... that claim to be able to support hundreds of concurrent connections because of the async I/O framework they're based on.... can someone give a few...
0
2215
by: asrock | last post by:
i have prepared a code in c++ to save high scores and it is working well when i compile it in turbo c++and run the code . but when i exit from the turbo c++ , the file to which i saved the high...
35
9298
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
2
11314
by: =?Utf-8?B?RGFtZW9u?= | last post by:
Hi - I am attempting to write lines to a file at high volume, multiple threads. Here is my scenario: (initial "WriteToFile" object created via a parent multithreaded process, which receives...
9
2919
by: pbd22 | last post by:
Hi. This is just a disaster management question. I am using XMLHTTP for the dynamic loading of content in a very crucial area of my web site. Same as an IFrame, but using XMLHTTP and a DIV. I...
4
5403
by: kyle christian | last post by:
I am trying to save the high scores of a game I made. Now Im stumped on trying to search through the file. Should I use a string, array, or one of the STL containers to manipulate the information...
8
3963
by: toefraz | last post by:
Hi, guys. I'm writing a little game, and I want to include a high scores list. I've got everything coded except for the sorting part. I want to use the std::sort() function, but I'm not quite sure...
3
2437
by: pozze | last post by:
Hi, I've just made the change from ASP to .net. I have a file (code below) that saves a user submitted file to a MS SQL 2005 database. It collects the file name, file size, file type, and lastly...
3
8860
by: hamishmcgee | last post by:
Ok, so for a project at university I have to create a High Score table in C++ with Visual Studio. Just to let you know this is my first year at university and also my first time ever learning C++....
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7089
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.