Preventing refresh

Discussion in 'PHP' started by ghadacr, Aug 7, 2007.

  1. #1
    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......
     
    ghadacr, Aug 7, 2007 IP
  2. Providence

    Providence Peon

    Messages:
    568
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Please elaborate. I don't think you can do that since you can click refresh on the browser or press F5
     
    Providence, Aug 7, 2007 IP
  3. syedwasi87

    syedwasi87 Active Member

    Messages:
    2,147
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    90
    #3
    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?
     
    syedwasi87, Aug 7, 2007 IP
  4. Providence

    Providence Peon

    Messages:
    568
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah. The question is not clear.
     
    Providence, Aug 7, 2007 IP
  5. ghadacr

    ghadacr Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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..
     
    ghadacr, Aug 7, 2007 IP
  6. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #6
    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 :)
     
    killerj, Aug 7, 2007 IP
  7. ghadacr

    ghadacr Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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...
     
    ghadacr, Aug 7, 2007 IP
  8. Providence

    Providence Peon

    Messages:
    568
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You can also use a session for each user and code something to prevent that user from re-entering the data twice.
     
    Providence, Aug 7, 2007 IP
  9. Vangs

    Vangs Peon

    Messages:
    8
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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:
     
    Vangs, Aug 7, 2007 IP
  10. Providence

    Providence Peon

    Messages:
    568
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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.
     
    Providence, Aug 7, 2007 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    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.
     
    nico_swd, Aug 7, 2007 IP
  12. ghadacr

    ghadacr Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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??????
     
    ghadacr, Aug 7, 2007 IP
  13. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #13
    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:
     
    krt, Aug 7, 2007 IP
  14. ghadacr

    ghadacr Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Cheers krt, thanks the simplified code... On that note that was a nice punch by gerrard two days ago....:p
     
    ghadacr, Aug 7, 2007 IP
  15. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #15
    Yup, surprised to not see a red card, it was in view of the ref who was only a few metres away :D

    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.
     
    krt, Aug 7, 2007 IP