SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Detect back button pressed?

    hi guys,

    I know you cant disabled the back button, and I don't really want to - but is there any way to stop a code block running again if a user clicks back?

    I am writing a quick and dirty (low budget) wizard in PHP for one of my clients and it post's back to itself until it follows X amount of steps. Everything is running as I'd wished but I just want to catch this eventuality.


    Many thanks.

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess it is more javascript issue: something like that?

    http://stackoverflow.com/questions/1...-in-javascript

    In PHP you can just check in session latest request of that user to see if they are the same, I guess.
    http://www.yourshoutbox.com <- FREE chat for your website - limited offer
    http://www.smartchatbox.com <- let your visitors talk

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,220
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    I haven't tried this (so consider it a theory), but if you set your Cache header to no-cache so the pages can't be cached, the browser might now allow the back button to be invoked, or if it did, it would show a message stating the page has expired and to refresh to get the latest version of it.

  4. #4
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    I haven't tried this (so consider it a theory), but if you set your Cache header to no-cache so the pages can't be cached, the browser might now allow the back button to be invoked, or if it did, it would show a message stating the page has expired and to refresh to get the latest version of it.
    Worth a shot

    What I really want is to distinguish between the submit button being pressed to go forward (postback), and the back button being pressed on the browser.

    I have an Object stored in a session variable which has data added to it with every step, if one were to go backwards the data would be added twice and thats what id like to avoid is possible.

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,220
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Matt Tacchi View Post
    Worth a shot

    What I really want is to distinguish between the submit button being pressed to go forward (postback), and the back button being pressed on the browser.

    I have an Object stored in a session variable which has data added to it with every step, if one were to go backwards the data would be added twice and thats what id like to avoid is possible.
    I can understand that The problem you will face (that hopefully using the Cache header with no-cache will solve) is the browser is serving a cached copy of the page, so your script isn't executing to load that previous step (so nothing in your script will block it), but you could check when the form is submitted again, does the data already exist and should I update it?

    Or you can try using no-cache and verify that the browser doesn't load a cached version to the user.

  6. #6
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    I can understand that The problem you will face (that hopefully using the Cache header with no-cache will solve) is the browser is serving a cached copy of the page, so your script isn't executing to load that previous step (so nothing in your script will block it), but you could check when the form is submitted again, does the data already exist and should I update it?

    Or you can try using no-cache and verify that the browser doesn't load a cached version to the user.
    Will give that a bash tommorow If any other PHP pro's have any suggestions as well they are most welcome

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Well, in the short term;
    Write a 'executed' code into the session; dont execute the code block if the session holds that value.
    This is obviously easier to do with form input.

    For example:
    Code:
     <input type='hidden' name='formid' value='<?php echo time(); ?>'>
    PHP Code:
    if(!isset($_SESSION[$_POST['formid']])) {
     
    //Execute.
     
    $_SESSION[$_POST['formid']] = '';

    If they push the back button (and thus, resubmit the form), $_SESSION will hold the value of formid already, and not execute the code.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  8. #8
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Well, in the short term;
    Write a 'executed' code into the session; dont execute the code block if the session holds that value.
    This is obviously easier to do with form input.

    For example:
    Code:
     <input type='hidden' name='formid' value='<?php echo time(); ?>'>
    PHP Code:
    if(!isset($_SESSION[$_POST['formid']])) {
     
    //Execute.
     
    $_SESSION[$_POST['formid']] = '';

    If they push the back button (and thus, resubmit the form), $_SESSION will hold the value of formid already, and not execute the code.
    Thats a damn good idea, im going to try that - will let you know how I get on!!


Bookmarks

Posting Permissions

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