Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: lock help plz

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    138
    Thanks
    0
    Thanked 0 Times in 0 Posts

    lock help plz

    hi

    i have about 1000 visitor /hits so
    i would like some help..how to lock and unlock these 2 files in the foloowing php script
    what would the final code b in this case?

    <?php

    $DATEN = "../leg1/log" ;
    $DATEZ = date("m.d.y");
    $DATEX = ".txt" ;
    $DATEW = $DATEN.$DATEZ.$DATEX ;
    if ( file_exists( $DATEW ))

    print "" ;
    else
    {
    touch($DATEW);
    }

    $filu = fopen( $DATEW,"a+");
    $ip = $_SERVER['REMOTE_ADDR'];
    $selain = $_SERVER['PHP_SELF'];
    $selref = $_SERVER['HTTP_REFERER'];
    $aikaleima = date("Y-m-d H:i:s");
    $kavija = "Timeserver: $aikaleima\nIP: $ip\nPAGE: $selain\nRef: $selref\n\n";
    fwrite($filu, $kavija);
    fclose($filu);

    ?>

    <?php

    $DXX = "../leg2/aa" ;
    $DYY = date("m.d.y");
    $DZZ = ".txt" ;
    $DXYZ = $DXX.$DYY.$DZZ ;
    $filename = $DXYZ ;

    if ( file_exists( $filename ))

    print "" ;
    else
    {
    touch($filename);
    }

    if(!file_exists($filename)){
    $fd = fopen ($filename , "w+");
    fclose($fd);
    }

    $file = file($filename);
    $file = array_unique($file);
    $hits = count($file);


    $fd = fopen ($filename , "r");
    $fstring = fread ($fd , filesize ($filename)) ;
    fclose($fd) ;
    $fd = fopen ($filename , "w");
    $fcounted = $fstring."\n".$hits;
    $fout= fwrite ($fd , $fcounted );
    fclose($fd);
    ?>


    plus any disadvangtage of locking the file..??
    Move on....

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    to use locks, put:

    flock($fd, LOCK_EX);

    before writing and

    flock($fd, LOCK_UN);

    when finished writing.


    Note that when a lock is set, a file may not be read or written to. for more info, see http://www.php.net/flock


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •