stylusrose Posted April 29, 2008 Share Posted April 29, 2008 I was trying to write a script that outputs to a file. the problem is, I don't want more than one user to be able to run that script at time. I know, mysql would probably be a better idea. But I'm just tinkering around with ideas. So, is there anyway to prevent a user to access a script whilst it is in use? Link to comment https://forums.phpfreaks.com/topic/103386-file-lock/ Share on other sites More sharing options...
skatermike21988 Posted April 29, 2008 Share Posted April 29, 2008 you can use mysql and then an if statement like if($inuse=="yes") { echo"Sorry the file is currently in use, please try again later"; } else { //DISPLAY DATA HERE } And then have like a cron job script run to where if the last time it was used was 10 mins ago or so, it clears the mysql, just incase the user exits his/her browser, and have a link to exit, so that way it clears the data. I know it sounds kinda confusing but I haven't been to sleep yet, so hope this helps Link to comment https://forums.phpfreaks.com/topic/103386-file-lock/#findComment-529440 Share on other sites More sharing options...
stylusrose Posted April 29, 2008 Author Share Posted April 29, 2008 hehe... I was actually wondering if there was a way to do it without mysql 0___0 Link to comment https://forums.phpfreaks.com/topic/103386-file-lock/#findComment-529444 Share on other sites More sharing options...
DarkWater Posted April 29, 2008 Share Posted April 29, 2008 Look into the flock() function. http://us2.php.net/manual/en/function.flock.php Link to comment https://forums.phpfreaks.com/topic/103386-file-lock/#findComment-529450 Share on other sites More sharing options...
kenrbnsn Posted April 29, 2008 Share Posted April 29, 2008 You can use the flock() function. At the start of the script, do <?php $fp = fopen('dummy.file','w'); if (flock($fp, LOCK_EX)) { // // do your work // } else { // issue the error message } fclose($fp); ?> Ken (beaten to it) Link to comment https://forums.phpfreaks.com/topic/103386-file-lock/#findComment-529451 Share on other sites More sharing options...
stylusrose Posted April 29, 2008 Author Share Posted April 29, 2008 oh, well that was easy o.o; thanks! ^.^ Link to comment https://forums.phpfreaks.com/topic/103386-file-lock/#findComment-529460 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.