Could you help me debug this PHP?

Hello Im a little new to debugging PHP.

Can anyone help me debug my PHP Counter script?

It works fine. I think it forgets to count sometimes…


<?php //start PHP script
$COUNT_FILE = "count_data.txt"; //load this txt file as the count file

if (file_exists($COUNT_FILE)) { //if count file exists...
	$fp = fopen("$COUNT_FILE", "r+"); //open it
	flock($fp, 1); 
	$count = fgets($fp, 4096);
	$count += 1; //add 1
	fseek($fp,0); 
	fputs($fp, $count);
	flock($fp, 3);
	fclose($fp); 
$message="$count";
	echo "result=$message"; //display Counter result
} else {
$message="???";
	echo "result=$message"; //display Error result
}
?> //end php script

<?php //start PHP script
$COUNT_FILE = "count_data.txt"; //load this txt file as the count file

if (file_exists($COUNT_FILE)) //if count file exists...
{
    $fp = fopen("$COUNT_FILE", "r+"); //open it
    flock($fp, LOCK_EX); 
    $count = fgets($fp, 4096);
    $count += 1; //add 1
    fseek($fp,0); 
    fputs($fp, $count);
    flock($fp, LOCK_UN);
    fclose($fp); 
	$message="$count";
}
else 
{
	$message="???";
}

echo "result=$message"; //display Counter result
?>

It was apparently the locks that caused the problem :slight_smile:

Wierd…thanks so much for making the right adjustments

I owe you one :slight_smile: