我想通过刷新防止双重插入 但我所做的并不起作用! 我哪里错了?
<?php
session_start();
$form_token = uniqid();
$_SESSION['user_token'] = $form_token;
?>
<form method="post">
<input type="hidden" name="user_token" value="<?php echo $_SESSION['user_token']; ?>" />
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit'])){
if ($_POST['user_token'] == $_SESSION['user_token']) {
echo "TEST OK";
unset($_SESSION['user_token']);
} else {echo "Session Expired";}
}
?>
答案 0 :(得分:0)
您需要对代码的顺序进行一些修改。
<?php
session_start();
// If this is a post request, you don't need to create a new session. Just check for the eixsting value.
if (isset($_POST['submit']))
{
if($_SESSION['user_token'] == $_POST['user_token']) {
echo "TEST OK";
} else {
echo "Session expired";
}
} else{ // Start a new session ONLY if this is the first time the user is entering this page.
$form_token = uniqid();
$_SESSION['user_token'] = $form_token;
}
include_once('./config.php');
?>
<form method="post">
<input type="hidden" name="user_token" value="<?php echo $_SESSION['user_token'];?>" />
<input type="submit" name="submit" value="Submit">
</form>
注意:此代码块仅用于帮助您找出双重插入问题(或者更确切地说是始终过期的会话&#39;输出)。但是,这可能不是在PHP中实现会话的最佳方式。您可能希望在PHP Sessions上阅读更多内容。查看官方文档注释中给出的示例。这应该可以让你对自己的成就有所了解,
答案 1 :(得分:0)
感谢DeadLock,我以这种方式解决了
session_start();
if (isset($_POST['submit']))
{
if($_SESSION['user_token'] == $_POST['user_token']) {
unset($_SESSION['user_token']);
$form_token = uniqid();
$_SESSION['user_token'] = $form_token;
include_once('./arrivi.php');
include_once('./script/controlloarrivi.php');
} else {
header("location: ./2.php");
}
} else{
$form_token = uniqid();
$_SESSION['user_token'] = $form_token;
include_once('./arrivi.php');
}
?>
现在我有另一个问题!我需要重新加载include_once('。/ arrivi.php');当我按提交 但我有结果 include_once( './脚本/ controlloarrivi.php');
我怎么能这样做?我读到我需要使用ajax!但是如何?