/    Sign up×
Community /Pin to ProfileBookmark

Managing flocks

I’m trying to wrtie some php which will produce a unique order reference number after a user has submitted my form, this number will start from 1000 and be incremented every time someone presses submit.

I have two different types of forms, ADSL and SDSL, so I need to manage the files that are locked as there could be more then one person trying to submit and write to the file and read the file yousee, to get the unique reference number.

I cant use a database so my only other option is a flatfile, and I cant just use uniqid or time stamp or microseconds either.

Has anyone got any ideas? please

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@bokehSep 23.2005 — Are you asking how to lock a file? If so the following is one way you could do it:[code=php]
$fp = fopen("file.ext", "r+");
$tries = 25;
while($tries > 0){
$locked = flock($fp, LOCK_EX);
if(!$locked){
usleep(100000);
$tries--;
}else{
$tries = 0;
}
}
if($locked){
fwrite($fp, $data);
fflush($fp);
flock($fp, LOCK_UN);
fclose($fp);
// no problem: no action needed
}else{
fclose($fp);
// problem: need to take action[/code]
Copy linkTweet thisAlerts:
@SheldonSep 23.2005 — oh is it to see what users have viewed the file? You cold run a small script that writes the username or ip to a seperste flat file?

as bokeh has suggested, inlcude this to the top of every page where you want to track.

[code=php]
<?php

$page_name = 'define each page differently';
$viewed = $_SESSION['username'];
$date = date(); //i cant do date :)

$fp=fopen("logs/log.php", "r");

while (!feof($fp)){
print stripslashes("$page_name, $viewed, $date");
}//end while

fclose($fp);

?>
[/code]


i wouldnt say that this works but maybe a starting point?
Copy linkTweet thisAlerts:
@bokehSep 23.2005 — oh is it to see what users have viewed the file? [/QUOTE]No! It's to try to avoid two or more instance of a script accessing the same file and either reading from or writing to it.
Copy linkTweet thisAlerts:
@SpectreReturnsSep 23.2005 — and I cant just use uniqid or time stamp or microseconds either.[/quote]
Why not?
Copy linkTweet thisAlerts:
@solidaritiauthorSep 24.2005 — Hi,

Could you please run through your code with me as im a new be. Does it allow for if two people try to access the same file at once, it will make the other person wait, until the file is open again?

$fp = fopen("file.ext", "r+");

$tries = 25;

while($tries > 0){

$locked = flock($fp, LOCK_EX);

if(!$locked){

usleep(100000);

$tries--;

}else{

$tries = 0;

}

}

if($locked){

fwrite($fp, $data);

fflush($fp);

flock($fp, LOCK_UN);

fclose($fp);

// no problem: no action needed

}else{

fclose($fp);

// problem: need to take action
Copy linkTweet thisAlerts:
@bokehSep 24.2005 — [code=php] $fp = fopen("file.ext", "r+"); // open file
$tries = 25; // max number of attempts
while($tries > 0){ // continue if less than 25 attepts have been made
$locked = flock($fp, LOCK_EX); // attempt to lock file
if(!$locked){ // if couldn't lock file
usleep(100000); //wait 100 milliseconds and try again
$tries--; // One try used up
}else{ // managed to get lock
$tries = 0; // file locked so no further tries needed
}
}
if($locked){ // if locked read/write file
fwrite($fp, $data); // data write
fflush($fp); // flush data to file
flock($fp, LOCK_UN); // release file lock
fclose($fp); // close file
// no problem: no action needed
}else{ // couldn't lock file
fclose($fp); // close it
// problem: need to take action[/code]
Copy linkTweet thisAlerts:
@solidaritiauthorSep 24.2005 — Thank you, so if there is a problem

}else{ // couldn't lock file

fclose($fp); // close it

// problem: need to take action

what do you mean by take action?
Copy linkTweet thisAlerts:
@bokehSep 24.2005 — what do you mean by take action?[/QUOTE]That's for you to decide. After all it's your script.
Copy linkTweet thisAlerts:
@solidaritiauthorSep 25.2005 — }else{ // couldn't lock file

fclose($fp); // close it

// problem: need to take action

i

So is this where I will make it go back to the while loop, to try again, as it cant lock the file to access it to write to it?

When the file cant be accessed i want it to try again. How would I do that?
Copy linkTweet thisAlerts:
@bokehSep 25.2005 — No! You will only reach this else conditional if 25 attempts have been made and failed. You then need to decide what to do as you were unable to get lock.
Copy linkTweet thisAlerts:
@solidaritiauthorSep 25.2005 — Thank you, for your help, I apologise for my niavity. Have a good week, I will implement this tomorrow, and let you know what happened.

Thank you once again

James
Copy linkTweet thisAlerts:
@Jeff_MottSep 25.2005 —  $tries = 25;

while($tries > 0){

$locked = flock($fp, LOCK_EX);

if(!$locked){

usleep(100000);

$tries--;

}else{

$tries = 0;

}

} [/quote]
If it failed the first time it's almost sure to fail the other 24 times. Perhaps instead...flock($fp, LOCK_EX) or die();

fflush($fp);

flock($fp, LOCK_UN); [/quote]
Both of these will happen automatically when you close the file.
Copy linkTweet thisAlerts:
@bokehSep 25.2005 — If it failed the first time it's almost sure to fail the other 24 times.[/QUOTE] What if the file is being used by another initiation of the script?
Copy linkTweet thisAlerts:
@Jeff_MottSep 26.2005 —  What if the file is being used by another initiation of the script?[/quote]Then flock waits until the existing lock is released. Basically flock will do on its own what you tried to do manually, except it will do it much more efficiently.
Copy linkTweet thisAlerts:
@solidaritiauthorSep 26.2005 — so if i use flock i dont need to manage it, as it does it automatically
Copy linkTweet thisAlerts:
@Jeff_MottSep 26.2005 — That's right.
×

Success!

Help @solidariti spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...