Accessible variable for file-locking

Discussion in 'PHP' started by chriseccles2, Sep 25, 2008.

  1. #1
    Another irritating noob question, I'm afraid !
    -
    Say that two or more users are running the same PHP script on my
    server from different locations. Is there any system-wide variable
    which I can set which is accessible to all, and which I can utilise to
    determine the lock status for a file ? (Ie: avoiding attempted
    simultaneous 'write' operations with a while loop in the script)
    -
    Thanks, guys. You seem to be patient with beginners.

    -
    Chris
     
    chriseccles2, Sep 25, 2008 IP
  2. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Hmm perhaps this is what your looking for?

    http://us.php.net/manual/en/function.flock.php
     
    Sillysoft, Sep 25, 2008 IP
  3. chriseccles2

    chriseccles2 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, sillysoft.
    -
    I saw that in the manual and was put off by the warnings about it
    possibly not working across multiple scripts running on the same
    server. How serious a warning is that if I have, say, 500 concurrent
    users on my server, all running a script called registerCust.php, and
    all wanting the customer username/password file ?
    -
    I only intend to lock/unlock ONE file, which will be a control file
    containing records which give the lock status of all the other files in my
    database. Once locked/unlocked status is inside a file record in the
    control, it's not vulnerable to corruption. Users can simply loop until
    they can get at the control file to find out whether they can read/write to
    the file the script wants to modify. The problem is locking/unlocking
    the control, itself, and this is what prompted my original post.
    -
    If anyone has solved this by means of devious logic and a globally-
    accessible persistent system variable, rather than the use of a filesystem
    function which may fail if stressed, I'd love to hear.
    -
    ps: My background is in FilemakerPro, C and some Perl.

    -
    Chris
     
    chriseccles2, Sep 25, 2008 IP
  4. linkexchange1984

    linkexchange1984 Peon

    Messages:
    73
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sillysoft,
    Nice Reply.................
     
    linkexchange1984, Sep 25, 2008 IP
  5. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Are you using flatafile database? How about creating a "lock" file ... used to do this with OpenVMS back in the days!!!

    eg script trying to update "filename.txt" would check if "filename.lock" exists ... if it does wait a sec and then check again ... create "filename.lock", update "filename.txt" with data and then delete "filename.lock" ...

    I think the danger with a while loop would be, if you have 500 scripts running simultaneously they may all try and create the lock file at the same time ...

    Why not use mySQL ... (I might be wrong but) maybe tehn you wouldn't need to worry about this ???
     
    mallorcahp, Sep 26, 2008 IP
  6. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #6
    are u using PHP File Functions?
    If yes, take a look at flock()

    php.net/flock

    sorry i am not allowed to post link yet
     
    ads2help, Sep 26, 2008 IP
  7. chriseccles2

    chriseccles2 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I can see you're from the same era as me. Ah, happy days !
    I got quite used to writing flat textfile databases in CBasic under CP/M
    and doing all my own indexing with trees read into arrays ........

    [pauses to put on Glenn Miller record and brown slippers ..] :)

    But, the individual lockfile idea is a good one. I'll pee around with
    some code and see what I get.

    I don't use SQL, mainly because (even after a good few years of this)
    I actually enjoy getting code to do quite low-level stuff !

    Thanks for the tip.

    -
    Chris
     
    chriseccles2, Sep 26, 2008 IP