One way is to insert a secret variable into the HTML specific to the user's session. This can prevent sites from being spoofed.
In PHP, you create a random “key” and save it in a session:
$_SESSION['myFormVar'] = md5(mt_rand());
Then in the form you add a hidden variable:
<input type="hidden" name="chkVar" value="<?=$_SESSION['myFormVar']?>"/>
You must submit your form via POST and preferably via HTTPS, which makes it difficult (but not impossible) to intercept the value of chkVar.
In the code that processes your published form, compare the published chkVar with your session variable. In an ideal world, you will have a unique chkVar for each request, however using one, which is the same for the whole session, often works fine and protects against most csrf attacks.
source share