我不知道或者是问题,我的代码不会一直错误1 ||错误2
<?php
session_start();
$token = uniqid(rand(), true);
$_SESSION['token'] = $token;
$_SESSION['token_time'] = time();
?>
<body>
<form id="form" name="form" method="post" action="">
<p>Pseudo :
<label>
<input type="text" name="pseudo" id="pseudo" />
</label>
</p>
<p>E-mail :
<label>
<input type="text" name="email" id="email" />
</label>
</p>
<p>Nom :
<label>
<input type="text" name="nom" id="nom" />
</label>
<input type="hidden" name="token" id="token" value="<?php echo $token;?>"/>
</p>
<p>
<label>
<input type="submit" name="Envoyer" id="Envoyer" value="Envoyer" />
</label>
</p>
</form>
</body>
</html>
我不知道或者是问题,我的代码不会总是错误1 || error2 我想保护我的网站,我不知道如何使用这个托根请帮帮我
<?php
在session_start();
if(isset($_SESSION['token']) && isset($_SESSION['token_time']) &&
isset($_POST['token']))
{
if($_SESSION['token'] == $_POST['token'])
{
$timestamp_ancien = time() - (15*60);
//Si le jeton n'est pas expiré
if($_SESSION['token_time'] >= $timestamp_ancien)
{
//here Instruction
}else{echo"error3";}
}else{echo"error2";}
}else{echo"error1";}
?>
答案 0 :(得分:0)
请在下面尝试:
<?php
session_start();
function generate_token(){
$token = uniqid(rand(), true);
$_SESSION['token'] = $token;
$_SESSION['token_time'] = time();
}
//avoid putting script at bottom until required
if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST)){
if(isset($_SESSION['token']) && isset($_SESSION['token_time']) &&
isset($_POST['token']))
{
if($_SESSION['token'] == $_POST['token'])
{
$timestamp_ancien = time() - (15*60);
//Si le jeton n'est pas expiré
if($_SESSION['token_time'] >= $timestamp_ancien)
{
//here Instruction
generate_token();//for next call
}else{echo"error3";}
}else{echo"error2";}
}else{echo"error1";}
}
generate_token();
?>
<body>
<form id="form" name="form" method="post" action="">
<p>Pseudo :
<label>
<input type="text" name="pseudo" id="pseudo" />
</label>
</p>
<p>E-mail :
<label>
<input type="text" name="email" id="email" />
</label>
</p>
<p>Nom :
<label>
<input type="text" name="nom" id="nom" />
</label>
<input type="hidden" name="token" id="token" value="<?php echo $_SESSION['token'];?>"/>
</p>
<p>
<label>
<input type="submit" name="Envoyer" id="Envoyer" value="Envoyer" />
</label>
</p>
</form>
</body>
</html>
现在发生的事情是每次加载页面时都会调用你的函数,但是发布数据会在早期状态下进行比较和操作。
**如果我理解错了,请纠正我。**