Page 1 of 1

refreshing

Posted: Sat May 25, 2002 12:07 pm
by hob_goblin
is there any way i could make it so when someone refreshes on a page where the submitted a form, that it does not submit again?

i saw a similar post, but it had to do with the back button

Posted: Sat May 25, 2002 12:12 pm
by volka
I made a suggestion on this topic here but haven't thought further about it. If this doesn't work (or you don't like it) let me know..... ;)

Posted: Sat May 25, 2002 12:13 pm
by EvilWalrus
set a "no-cache" and "expire" header in the script... that'll help with the back button part of it, but as for refreshing, not quite sure...

Posted: Sat May 25, 2002 1:33 pm
by hob_goblin
hmm, i thought about just redirecting to itself, but, it wont work correct...because "headers already are being output"... :roll:

would output buffering let me do this? i never really understood it

Posted: Sat May 25, 2002 3:07 pm
by jaybee
I redirect to receipt pages after an action query so that the page with the action in it can't be refeshed.....don't know if i'm missing the point...?

Posted: Sat May 25, 2002 3:17 pm
by hob_goblin
well its like a tagboard/chatroom type thing...

so i didnt want a reciept or thankyou page...

and since its being included into another file, it already outputs stuff before the header would be outputted

Posted: Sat May 25, 2002 3:40 pm
by NetDragonX
Well, why not just have it set a variable or cookie when the user clicks on the submit button. Then use a simple IF function to check if that variable/cookie exists.

Might even try using JavaScript to set the cookie rather then PHP.

Just an idea.

Posted: Sat May 25, 2002 4:19 pm
by jason
hob_goblin: If you are having problems with header() because of that error, use the ob_start() function at the top of all your scripts that you need it on ( I just put it in my config.php file) and you won't have to worry about the error.

Posted: Sat May 25, 2002 6:42 pm
by lc
Well I basicly figure I do it quite a bit simpler than most of that.

Just write a check before the script bit enters the data in the file.

Say $include is the new data as it will be written in the file and $last is the last line added to the file before. Thus after the data has been entered and someone then does a refresh... they should be exactly the same.

So simply do a:

if ($include != $last){
go and write it in
}

and voila...

That's how I have it running in a little GB I wrote and it works perfect.

Posted: Sat May 25, 2002 8:25 pm
by Tiimmy
A cookie would definately stop the data being entered into a DB or whatnot twice. But unfortunately I can't think of anyway to stop the browser from re-submitting the form data, Javascript might have some sort of object that prevents it, but I've never encountered it before...

Code: Select all

<?php

// HTML Form data.

if ($submit AND !isset($Form_Sent)) {
    setcookie("Form_Sent", $User, time() +3600 * 24 * 365 * 10,"/");
    // Process Form data.
    // Inform user that data has been successfully been processed.
}
else {
    // Inform user that data has already been sent.
    exit;
}

?>
Hmm....that's a bit of a pain; it won't let me use && in my conditional statement, just prints & instead...

Posted: Sun May 26, 2002 7:59 pm
by gotDNS
There is:

Code: Select all

<?php
session_start();

if(!session_is_registered("didpoll"))
	$didpoll="false";

$connect = mysql_connect("localhost:3306", "esites") && mysql_select_db("polls")
or $failed = "Could not connect to database.";

if($despoll && $didpoll!="true")
&#123;
	$didpoll="true";
	mysql_query("insert into layout set rating="$despoll"");
&#125; else &#123; $pollerror = "ERROR: You can only vote once!"; &#125;
?>

Posted: Sun May 26, 2002 8:59 pm
by Tiimmy
Sessions would only work until the user closes their browser. If that's all you wanted to achieve, that's fine. But to ensure that they don't vote until you allow them, use cookies. Yes, they can delete the cookie, but there's nothing that can be done to prevent it. :P