Make sure that the file_get_contents is successful (not returning false) before proceeding to write (otherwise the counter will be re-set as 1, which is not desirable)
Hence:
<?php
$count="counterfile.txt";
$var1=file_get_contents($count);
if ($var1!=false){
file_put_contents($count, (1+$var1) ,LOCK_EX);
}
?>
Alternatively, you may use a db approach (increment a field value by one each time)
solved A trivial counter fails in PHP