我有一个完整的代码,其中我setcookie并在表单提交我匹配它与发布的数据,但它无效但是当我在setcookie之后使用echo命令它的工作请检查下面的代码。当你在setcookie工作之后取消注释echo命令时,请自行尝试并请给我解决方案。我使用xampp,我在两个diff-diff xampp
上尝试 <?php
$msg='';
if(isset($_POST['reset_password'])){
if (isset($_POST["CSRFtoken"], $_COOKIE["CSRFtoken"])){
if ($_POST["CSRFtoken"] == $_COOKIE["CSRFtoken"]){
$msg = 'yes';
}else{
$msg ='invalid request';
}
}
}
$token = bin2hex(openssl_random_pseudo_bytes(16));
setcookie("CSRFtoken", $token, time() + 60 * 60 * 24);
//echo 's';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href=" ">
<title>Fitness - Free Bootstrap 4 Template</title>
</head>
<body>
<?php if($msg!=""){echo $msg;} ?>
<form action="" method="post">
<div class="form-group">
<input type="text" name="user" id="user" class="form-control" placeholder="Email/Mobile"/>
<input name="CSRFtoken" id="CSRFtoken" type="hidden" value="<?php echo $token; ?>">
</div>
<div class="form-group">
<input type="submit" name="reset_password" value="Reset" class="btn btn-primary">
</div>
</form>
</body>
</html>