I have entered the following code to prevent CSRF but issuing and checking tokens. The top section goes on the login.php, the second part goes on the landing page. The issuing of the token works, and when I print $_SESSION['token']on the landing page they match up. However, when i substitute the other code in, its says that they don't match and shows 'expired'.
<?php session_start(); $_SESSION['token'] = $token; $_SESSION['token'] = uniqid(md5(microtime()), true); print $_SESSION['token']; ?> <html> <head> <title>My first PHP website</title> </head> <body> <h2>Please login here to see your tour</h2> <form action= "checklogin.php" method="post"> Enter Username: <input type="text" name="username" required="required"/> <br/> Enter Password: <input type="password" name="password" required="required" /> <br/> <input type="hidden" name="token" value="<?php echo $_SESSION['token'] ?>" /> <input type="submit" value= "login" /> </form> </body> <?php session_start(); print $_SESSION['token']; session_start(); if ($_POST['token'] !== $_SESSION['token']) { die('expired'); } ?>
$_GET['token']but your form uses thePOSTmethod so you should use$_POST['token']instead, i.e.if ($_POST['token'] !== $_SESSION['token'])session_start();lines in your second (validating) file?print $_POST['token']inlogin.php.