Page 1 of 1
header - preventing resubmissions with browser refresh?
Posted: Sat Jun 21, 2008 5:54 pm
by josh22x
Hey.... I am unsure exactly how to prevent resubmissions into my database when I refresh the browser. I am using a mysql insert statement. So if I enter text in the form and click submit it is added to the database; however if I refresh the browser on that page the same text is duplicated in the database?
I have been trying to use:
Code: Select all
header('Location: ' . $_SERVER['REQUEST_URI']);
But when I use this or some combination of this it says headers already sent out. I have made sure that there is no extra white space and all of that.
How do I use this code or something else to keep the browser from resubmitting the text into the database?
Thanks in advance!
~Josh
Re: header - preventing resubmissions with browser refresh?
Posted: Sat Jun 21, 2008 6:52 pm
by Kieran Huggins
You could always check to see if there's a duplicate row in the DB first, but that still doesn't address the problem of the headers already being sent. Something is being sent, somewhere.
You could use output buffering to get around that:
http://www.php.net/manual/en/ref.outcontrol.php
Re: header - preventing resubmissions with browser refresh?
Posted: Sun Jun 22, 2008 1:57 pm
by josh22x
If there any way that you could offer me an example or something on exactly how to accomplish this? I am new to PHP and this is on area that is really confusing to me. Here is the snipping of the code I am trying to keep from resubmitting:
Code: Select all
function add_blog($username, $title, $content)
{
$connect = db_connect();
if(!$connect)
{
echo mysql_errno() . ": " . mysql_error() . "\n";
}
$con = mysql_query("insert into blogs values ('', '".$username."', '".$title."', '".$content."', CURRENT_TIMESTAMP())");
if(!$con)
{
echo mysql_errno($connect) . ": " . mysql_error($connect) . "\n";
}
else
{
echo 'Sumission sucessful!';
}
}
Re: header - preventing resubmissions with browser refresh?
Posted: Sun Jun 22, 2008 4:13 pm
by josh22x
I tried using a form of server incrementation, but it is not working. What am I doing wrong here?
Thanks!
Code: Select all
<?php
session_start();
$_SESSION["count"] = '0';
check_valid_user();
$username = $_SESSION['valid_user'];
$title=$_POST['title'];
$content=$_POST['content'];
if (isset($_SESSION["count"]))
{
add_blog($username, $title, $content);
select_form($title, $content);
unset($_SESSION["count"]);
user_menu();
html_footer();
}
else
{
select_form($title, $content);
user_menu();
html_footer();
}
?>
Re: header - preventing resubmissions with browser refresh?
Posted: Sun Jun 22, 2008 8:07 pm
by Kieran Huggins
I'll bet the header problem is because you're echoing 'Sumission sucessful!' - remove that and see if the header problem is fixed.
Re: header - preventing resubmissions with browser refresh?
Posted: Mon Jun 23, 2008 10:53 am
by josh22x
Ok, well I got the browser to stop saying header already sent out by using the ob_start(); and ob_flush(). I placed the ob_start() at the beginning of the html and css in the page and the placed the ob_flush() after my header redirct statement. However the redirect is not preventing resubmissions, actually it is not working at all. How do I get this to work?
Code: Select all
<?php
$username=$_SESSION['correct_user'];
$passwd = $_POST['password'];
$title=$_POST['title'];
$content=$_POST['content'];
check_valid_user();
blog_form($username, $password);
add_blog($username, $title, $content);
select_blog($title, $content);
menu();
footer();
header('Location: ' . $_SERVER['submission.php']);
ob_flush();
?>
Thanks!
Re: header - preventing resubmissions with browser refresh?
Posted: Tue Jun 24, 2008 2:46 pm
by josh22x
Anyone got an idea how I can do this?
Thanks!
Re: header - preventing resubmissions with browser refresh?
Posted: Tue Jun 24, 2008 4:25 pm
by WebbieDave
It seems your goal is to prevent the reposting of data by the visitor reloading the page. The common approach for dealing with this is to have the processing page (the page inserting the records) sending only a redirect header (back to the form page) rather than any html.
form page -> processing page -> form page
If the redirect is correctly being sent, then the visitor will be unable to insert additional rows by simply clicking the reload button. Check to make sure the browser is receiving the Location header.
Re: header - preventing resubmissions with browser refresh?
Posted: Fri Jun 27, 2008 5:04 pm
by josh22x
How do I do this: "Check to make sure the browser is receiving the Location header."?
Thanks!
Re: header - preventing resubmissions with browser refresh?
Posted: Tue Jul 01, 2008 7:12 am
by josh22x
Any idea?
Thanks!
Re: header - preventing resubmissions with browser refresh?
Posted: Tue Jul 01, 2008 7:30 am
by parimala
call the function when the user click on the submit button only.
for example submit button name is txtsubmit
if(isset($_POST[txtsubmit]))
{
function add_insert()
{
insert statement
}
}
thanks and regards
pari