Is this code synchronized?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
coltsith
Forum Newbie
Posts: 4
Joined: Tue Feb 12, 2008 2:17 am

Is this code synchronized?

Post by coltsith »

Hi there,

I have a php script for a highscore table for a game I'm making. The basic flow of the code is this:

Code: Select all

Open highscore file (READ mode)
-read values
 
Open highscore file (WRITE mode)
-write new highscore
 
close highscore file (READ mode)
close highscore file(WRITE mode)
I'm doing this because my concern is that if I do this:

Code: Select all

Open highscore file (READ mode)
-read values
close highscore file (READ  mode)
 
Open highscore file (WRITE mode)
-write new highscore
close highscore file(WRITE mode)
That if two players submit a highscore at the same time, they could potentially screw each other up if one read, the other read, then the first one wrote, and the second one wrote, etc.

Is this okay for me to do? If I'm not making sense I can clarify.

Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Is this code synchronized?

Post by Christopher »

You may want to look at file locking (flock()).
(#10850)
coltsith
Forum Newbie
Posts: 4
Joined: Tue Feb 12, 2008 2:17 am

Re: Is this code synchronized?

Post by coltsith »

Okay thanks!
Post Reply