title: How to prevent other websites from sending post data to my site in PHP
body: <p>What is the best method to stop other websites from sending data to my website in PHP?</p>
  <p>I googled it and found I need to use Hash. But there are different Hashes, which one to use? Let's say, I pick <code>sha1 </code></p>
  <p>Now, how exactly can I prevent other websites from sending post data to my website with <code>sha1</code></p>
  <p>I am bit confused, can someone show me a little demo code.</p>
  <p>This is the code, I thought, but it is not flawless..</p>
  <p><code>Index.php page:</code></p>
  <pre><code>$password = sha1(&quot;toby123&quot;);
  
  &lt;form method=&quot;post&quot; action=&quot;insert.php&quot; /&gt;
  &lt;input type=&quot;text&quot; name=&quot;username&quot;/&gt;
  &lt;input type=&quot;password&quot; name=&quot;password&quot;/&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;hiddenpass&quot; value=&quot; &quot;.$password.&quot; &quot;/&gt;
  &lt;/form&gt;
  </code></pre>
  <p>Insert Into Database PHP Page:</p>
  <pre><code>$hiddenpass = &quot;toby123&quot; 
  
  if (  $_POST[&quot;hiddenpass&quot;] == &quot;sha1($hiddenpass )&quot;  )
  
  {
  // INSERT THE DATA
  }
  </code></pre>
  <p>But the problem here is, <code>hash</code> code in the form will be visiable to everyone. What if someone crack it?</p>
  <p>I mean, by using a Hit &amp; Trial method???</p>
  <p>Is my method 100% safe??</p>
  <p><strong>Edit:</strong></p>
  <p>This is my new code after looking at one of the answer, but the If condition is always false.</p>
  <p>Index.php Page</p>
  <pre><code>&lt;?php
  // Start the session
  session_start();
  
  
      $token = bin2hex(random_bytes(32));
      
      if (!isset($_SESSION['token'])) {
          $_SESSION['token'] = $token;
      }
  
  ?&gt;
  </code></pre>
  <p>Insert.php Page:</p>
  <pre><code>session_start();
  
  echo $_SESSION['token'];
  echo '&lt;br/&gt;';
  echo $_POST['token'];
  
  if (  ( isset($_SESSION['token']) )  &amp;&amp;  ( $_POST['token'] == $_SESSION['token'] ) )
  
  {
  
  // Insert Data
  
  }
  else
  echo &quot;&lt;br/&gt;Something went wrong&quot;;
  
  
  unset($_SESSION['token']);
  </code></pre>
  <p><strong>Output:</strong></p>
  <blockquote>
  <p>055442be59701631db6ed88dc341027ebf2238507bb9a72f1caefd6d3b126a4b</p>
  <p>055442be59701631db6ed88dc341027ebf2238507bb9a72f1caefd6d3b126a4b</p>
  <p>Something went wrong</p>
  </blockquote>