title: Easiest way to create a unique code to use between Javascript and PHP?
body: <p>I'm looking for a way to create the same code in both JavaScript and PHP as a security measure to make sure you can't just access the PHP script, but only when this JavaScript says to access it.</p>
  
  <p>I am using AJAX to access a PHP logging file</p>
  
  <pre><code>var xhttp = new XMLHttpRequest();
  xhttp.open('POST', 'log/', true);
  xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  xhttp.send('uniqueCode=xxxxxxxx...');
  </code></pre>
  
  <p>(where <code>xxxxxxxx...</code> is the unique code.)</p>
  
  <p>In the PHP code I want to add</p>
  
  <pre><code>if ($_POST['uniqueCode'] === xxxxxxxx...) {
      //Run Code
  }else {
      http_response_code(403);
  }
  </code></pre>
  
  <p>(where <code>xxxxxxxx...</code> is the unique code.)</p>
  
  <p>Is there a way to transfer this unique code beforehand and then use the above code?</p>
  
  <p>I am using PHP Version 5.6</p>