Has any one come up with a solution in, trying to prevent refresh of a page.. without using the MVC model approach.. e.g. view - process - view......
Please elaborate. I don't think you can do that since you can click refresh on the browser or press F5
i think he is talking from a php 'context', i am also not a php biggie so cant tell.. are you talking as in a refreshing a page in a script?
Sorry let me clarify, If you insert some data into a database, then you get the result page, but if you press refresh then the same data is then retentered into the database.. Basically i want to prevent that.. without the need for a redirect to another page..
i think you use GET method when you say "redirect to a new page" Using POST method will allow your members or users who submit the form to remain in the same page . Keep in mind to Check for Duplicates before entering anything(INSERT or UPDATE) into database. I hope this will give you the general concept of what has to be done. good luck
Gracias, for the suggestions but i was kinda of hoping for some example code, hmmm i know that is a bit forward, but any code that is posted i can use and then build on from there and refine it to suit my application...
You can also use a session for each user and code something to prevent that user from re-entering the data twice.
A good method is to redirect after a successful post action. A redirection to another page using header which will prevent people refreshing with the POST data. No more duplicate entries. if(!empty($_POST)) { //perform error checking if(!isset($error)) { //no errors in form so perform queries then redirect header('Location: http://example.com/success.html'); } } //display form PHP:
Yep that's a good way too. There are just so many ways to do this. I think it will just be ghadacr who can decide what he wants to use.
He said he didn't want that. However, you may consider redirecting to the SAME page after inserting the data. If you want code, we'd need your actual code to modify it. (It's hard to guess). What you could do is, generate an MD5 value, made of all data that the user submits. $hash = md5($username . $password . $message . $whatever); PHP: And store it with each submission. Then, before inserting the data check if this MD5 exists in the database, and only insert if not.
Thanks everyone for making the suggestions, its one of those questions where programmers are going to differ on... This is what i'm going to do, i'm going to use sessions. But i'm kinda of stuck on how to start it... for instance.. if the session is equally to true then should it be redirected or prevent the query from being executed??????
A simplified version of what I use: if (($hash = md5(join('', $_POST))) == $_SESSION['last_action']) { exit('Duplicate request'); } $db->query(... // do whatever // after actions are committed: $_SESSION['last_action'] = $hash; PHP:
Cheers krt, thanks the simplified code... On that note that was a nice punch by gerrard two days ago....
Yup, surprised to not see a red card, it was in view of the ref who was only a few metres away To keep this thread on topic: Let me know if there are any issues with that code. My first few tries using that idea resulted in some false positives. In fact, I just thought of one, that first conditional should only be checked if there is some POST data. Also, you may want to use $_REQUEST so that both GET and POST data is accounted for.