Anti-CSRF code giving unexpected results.
Posted: Wed Dec 06, 2006 10:25 am
I've been polishing up my security with forms today and I've come across a chapter which shows me how to avoid CSRF attacks. I've learned to use the following code to avoid these attacks but I'm getting unexpected results. Please can somebody point out why on each form submittion the results of the 2 variables never match:
$_POST['check'] and $_SESSION['token'] never match.
Regards, Stephen
Code: Select all
session_start();
$_SESSION['token'] = md5(uniqid(rand(), TRUE));
echo "<form method = 'post' action = 'uni.php'>";
echo "<input type = 'hidden' name = 'check' value = '$_SESSION[token]'>";
echo "<input type = 'text' name = 'one'>";
echo "<input type = 'submit' value = 'Submit'>";
echo "</form>";
if (isset($_POST['one'])) {
if ($_SESSION['token'] == $_POST['check']) {
echo "Match";
}
}
echo $_POST['check'];
echo "<br>";
echo $_SESSION['token'];Regards, Stephen