...

View Full Version : lock help plz



kab_184
11-01-2003, 03:05 PM
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..??

missing-score
11-01-2003, 04:22 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum