If all you need the database for is tracking hit counts, then you'd be better off using a file as you are doing now. What you are missing is flushing the file to disk before releasing the lock. See http://nz.php.net/flock
while(!$fp=fopen($filename,"a+")) usleep(100000);
while(!flock($fp,LOCK_EX)) usleep(100000);
while(!include($filename)) usleep(100000);
ftruncate($fp,0);
rewind($fp);
$$varname=$newval;
//$text="text changes depending on the $type value";
fwrite($fp,$text);
fflush($fp);
flock($fp,LOCK_UN);
fclose($fp);