foxphpbomb
Programmer
I would like to know if there is some code that I can use to prevent a page that has been loaded in which the values from a form are inserted in a db, from entering a duplicate record when the refresh button is hit,
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
session_start();
if(!isset($_SESSION['uniqueid'])){
$_SESSION['uniqueid']="someuniqueid";
[green]\\and perform updates. or insterts[/green]
}
else{
[green]\\issue message of duplicate entries and redirect.[/green]
}
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
<?php
[b]for[/b] ($_POST as $field=>$value) {
[gray]// check the data[/gray]
}
[b]if[/b] ($error_count==0) {
[gray]// save the data[/gray]
header([i]"Location: thank_you.htm"[/i]);
[b]return[/b];
}
?>
<html>
<head>
<title>You have <?php echo $error_count; ?> error(s) in the form data</title>
</head>
[gray]<!-- ... -->[/gray]
<?php
session_start();
if (isset($_POST['submit'])){
processForm();
}else{
displayForm();
}
function testUnique(){
if (!isset($_SESSION['uid'])) return false;
if (!isset($_POST['uid'])) return false;
if (trim($_POST['uid']) !== $_SESSION['uid']) return false;
return true;
}
function generateUID(){
if (isset($_SESSION['uid'])){
return $_SESSION['uid'];
}else{
$_SESSION['uid'] = uniqid("test_", true);
return $_SESSION['uid'];
}
}
function displayForm($msg=''){
$msg = !empty($msg)?"<legend>$msg</legend>":'';
$uid = generateUID();
$field = isset($_POST['field']) ? $_POST['field'] : '';
echo <<<HTML
<form method="post" action="{$_SERVER['PHP_SELF']}" >
<fieldset style="width:40%;">
$msg
<input type="hidden" name="uid" value="$uid" />
<input type="text" name="field" value="$field" />
<input type="submit" name="submit" value="submit" />
</fieldset>
</form>
HTML;
}
function processForm(){
if (testUnique()){
//processform
//if form processes correctly kill the session vars
killVars();
displayForm("success");
} else {
//you may want to do something else here
killVars();
displayForm("refresh");
}
}
function killVars(){
unset($_SESSION['uid']);
unset($_POST);
}
?>