title: Visits counter without database with PHP
body: <p>I have a single webpage and i would like to track how many times it's visited without using a database.</p>
  
  <p>I thought about XML, updating a file every time a user visits the page:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;counter&gt;8&lt;/counter&gt;
  </code></pre>
  
  <p>Then i thought it could have been a better idea to declare a PHP counter in a separate file and then update it everytime a user visits the page.</p>
  
  <p>counter.php</p>
  
  <pre><code>&lt;?php
      $counter = 0;
  ?&gt;
  </code></pre>
  
  <p>update_counter.php:</p>
  
  <pre><code>&lt;?php
      include "counter.php";
      $counter += 1;
      $var = "&lt;?php\n\t\$counter = $counter;\n?&gt;";
      file_put_contents('counter.php', $var);
  ?&gt;
  </code></pre>
  
  <p>With this, everytime <code>update_counter.php</code> is visited, the variable in the <code>counter.php</code> file is incremented.</p>
  
  <p>Anyway, i noticed that if the <code>counter.php</code> file has <code>$counter = 5</code> and the <code>update_counter.php</code> file is visited by i.e. 1000 users at the exact same time, the file gets read 1000 times at the same time (so the the value <code>5</code> gets read in all requests) the <code>counter.php</code> file will be updated with value <code>5+1 (=6)</code> instead of <code>1005</code>.</p>
  
  <p>Is there a way to make it work without using database?</p>