refreshing

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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

refreshing

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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..... ;)
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

Post 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...
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
User avatar
jaybee
Forum Newbie
Posts: 12
Joined: Tue May 14, 2002 3:58 pm
Location: brighton, uk

Post 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...?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
User avatar
NetDragonX
Forum Newbie
Posts: 15
Joined: Sat May 25, 2002 3:00 pm
Location: Southern California

Post 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.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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.
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post 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.
Tiimmy
Forum Commoner
Posts: 38
Joined: Sat Apr 27, 2002 1:56 am
Location: Australia
Contact:

Post 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...
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post 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;
?>
Tiimmy
Forum Commoner
Posts: 38
Joined: Sat Apr 27, 2002 1:56 am
Location: Australia
Contact:

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