473,799 Members | 2,885 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 3529
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
3664
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 examples of some web sites actually using those Python frameworks? Thanks :) --Irmen
0
2229
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 scores get corrupted and so when i again takes the turbo c++ , the files which i have written to the file are all missing . please help me . here is the code #include<iostream.h> #include<fstream.h> #include<string.h> #include<conio.h> struct...
35
9367
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 500000 to 3200000 of a file whose size is say 20MB... how do i request a download which starts directly at 500000th byte... thank u cheers
2
11327
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 files, can call custom code for each file received - much like BizTalk) 1. Receive multiple files, each file received creates a "WriteToFile" object
9
2986
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 got the core of the javascript from here: http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm I noticed in the demo that sometimes the content takes a long
4
5421
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 read from the file? I have tried the stringname.find("Name"); This only returns the first occurence, and doesn't look for other occurences.
8
3989
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 how I could get it to work with the data in my vector. Here is a sample high score file in which each line is inputted into a vector element. I want to sort by the score: 2345 toefraz day time year 1009 toe day time year...
3
2474
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 the binary data for the file. I can sucessfully take the files out of the databse again and display them in a data grid. I would like to resize the submitted file to a fixed size (say 180 x 120) before I upload it to the database and do this without...
3
8904
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++. So far I have a working menu, which lets you enter in your name, highscore and date which then gets saved to a file and is outputted via another option on the menu. I had a lot of trouble gettin the user info to save to a file in the first place, then...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10249
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10025
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6804
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4138
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 we have to send another system
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2937
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.