header - preventing resubmissions with browser refresh?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
josh22x
Forum Commoner
Posts: 38
Joined: Tue Feb 26, 2008 11:17 am

header - preventing resubmissions with browser refresh?

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: header - preventing resubmissions with browser refresh?

Post 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
josh22x
Forum Commoner
Posts: 38
Joined: Tue Feb 26, 2008 11:17 am

Re: header - preventing resubmissions with browser refresh?

Post 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!';
    }
}
 
josh22x
Forum Commoner
Posts: 38
Joined: Tue Feb 26, 2008 11:17 am

Re: header - preventing resubmissions with browser refresh?

Post 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();
    }
 
?>
   
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: header - preventing resubmissions with browser refresh?

Post 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.
josh22x
Forum Commoner
Posts: 38
Joined: Tue Feb 26, 2008 11:17 am

Re: header - preventing resubmissions with browser refresh?

Post 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!
josh22x
Forum Commoner
Posts: 38
Joined: Tue Feb 26, 2008 11:17 am

Re: header - preventing resubmissions with browser refresh?

Post by josh22x »

Anyone got an idea how I can do this?


Thanks!
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: header - preventing resubmissions with browser refresh?

Post 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.
josh22x
Forum Commoner
Posts: 38
Joined: Tue Feb 26, 2008 11:17 am

Re: header - preventing resubmissions with browser refresh?

Post by josh22x »

How do I do this: "Check to make sure the browser is receiving the Location header."?

Thanks!
josh22x
Forum Commoner
Posts: 38
Joined: Tue Feb 26, 2008 11:17 am

Re: header - preventing resubmissions with browser refresh?

Post by josh22x »

Any idea?

Thanks!
parimala
Forum Newbie
Posts: 8
Joined: Wed Jun 11, 2008 7:21 am

Re: header - preventing resubmissions with browser refresh?

Post 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
Post Reply