Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts

    Question Preventing a browser refresh from reposting

    I've searched for solutions on how prevent a browser refresh from posting a repeat process, and a suggestion of redirecting the page to a "dummy" page which then flips back to the main page seems like something I can handle.

    My problem is, I'm not quite clear on the journey. Here's what I understand:

    Page loads, waits for user input
    User fills out form and hits "submit"
    Page inits, with $_POST variable populated with form data (I have a db_start() up top)
    PHP code is processed, at which point I take care of form data

    here is where I'm not sure how the redirect should happen. I'm thinking:
    Before page is released to browser, I redirect the page to the "dummy" page with something like this:

    PHP Code:
    $host  $_SERVER['HTTP_HOST'];
    $uri  rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra 'dummy.php';
    header("Location: http://$host$uri/$extra");
    ob_end_flush();
    exit; 
    Finally, the dummy page has a single header command to reload, sans $_POST variable, the main page.

    Do I have the right idea? Or is there a better way?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Do I need to rephrase this question?

  • #3
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Your POST page should send the data to a processing page. This page should not return anything to the user-agent (users browser). It should instead redirect to another page that sends output to the user-agent.
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

  • #4
    Regular Coder
    Join Date
    Feb 2006
    Location
    The Netherlands
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think you're doing it well, but I think an easier and more valid way of doing this is this:

    Upon posting the form, save the current time in a $_SESSION variable,

    PHP Code:
    $_SESSION['posted'] = strtotime('now');// Will contain the time in seconds since January 1970 
    Then you can check if the user has posted within a specified limit of time,

    PHP Code:
    $currtime strtotime('now');

    if(empty(
    $_SESSION['posted'])){
    $prevtime 0// Big difference between current and previous time
    }
    else{
    $prevtime $_SESSION['posted'];
    }
    $timediff $currtime $prevtime;
    $limit 500// Time in seconds there has to be between two posts
    if($timediff $limit){

      
    // Put your actions here


    Yeah that.

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    hemebond-- That was the ticket, works great! thanks. Now, what mechanism would you use to send data from the processing page back to the display page? Let's say there's an error in the processing, and I need to let the user know about the error. The only thing I can think to do is use a query string on the url, but that might get messy-- especially if I am using a query string on the display page for other things already. Any other thoughts on that?

    Vin0rz-- your technique worked too-- a little too well! I don't want to prevent a user from performing actions on the form many times quickly in succession, as long as they are legit actions-- I just want to disable the "refresh" issues, and a separate processing page seems to do the trick.

  • #6
    Regular Coder
    Join Date
    Feb 2006
    Location
    The Netherlands
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, if the processing page did not output anything then you'd be able to send headers I guess... Or don't I understand the question?
    Yeah that.

  • #7
    Regular Coder
    Join Date
    Dec 2005
    Posts
    346
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Vin0rz
    Well, if the processing page did not output anything then you'd be able to send headers I guess... Or don't I understand the question?
    I think he is asking on the processing page if you have php checking to see if a field is blank & it blank then it shows the page again to have the field filled in. Then when the user fills in the field & resubmits the form it wont let him because he has to wait 500 seconds to resubmit the form.

    What about if we have this line $_SESSION['posted'] = strtotime('now'); right before the line that says Submission of form successfull...would that work? If we put that line above as I just stated then if the php says the field is blank & sends you back to fill in the field then the script never processed as far as to say Submission of form successfull & so the $_SESSION['posted'] = strtotime('now'); never got set...right?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •