how do i prevent resending of information when hitting the reload/refresh button?
i have a php script with apache server and runs in a linux box, fedora core 2 in particular.
the problem is that, i have a login form which uses session and post method and searches the mysql database to match the username/password, when i click on the logout button, logout.php is called which destroys the session. but when i hit the back button and reload it. there is a message that a POST DATA that expires from the cache is to be resend, and if i click okay, the page will reload and as if the user is logged in again... how do i correct this? please help...

    What I do is immediately after processing the posted information, I do a redirect via the Location header. If you want them to stay on the same page you can have it redirect back to itself.

    doLogin($_POST['username'], $_POST['password']);
    Header('Location: '.$_SERVER['PHP_SELF']);
    exit();
    

      thank you very much. but i tried what you said but it doesn't redirect me to another page... what does the header function do? does it automatically display the page you specify?

        The header function simply sends an http header to the browser. It must be called before any output is sent to the browser. (In this case you wouldn't be outputing anything else to the page, at least not in this script.) The exit function just exits the script, of course.

        When you send a location header to the browser you are telling the browser to redirect to the new url. So if you wanted to redirect to page2.php, you would do this

        header('Location: page2.php');
        exit();
        

        The example I gave before was to redirect the user back to the same page. I do this when updating a cart, or something like that, where I want the user to remain on the page, but I don't want refreshing the page to resubmit a form.

          I think that you are not ending the seesion fully when you process the logout. You also need to destroy the session cookie if you don't want the login to ghost back.

          
          //--- LOG OUT button clicked --------------------------------------------------
          if (isset($_REQUEST['log_out'])) {  
          // Unset all of the session variables. $_SESSION = array(); // If it's desired to kill the session, also delete the session cookie. // Note: This will destroy the session, and not just the session data! if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time()-42000, '/'); } // Finally, destroy the session. session_destroy(); } //--- END LOG OUT ------------------------------------------------------------

          All credit to Weedpacket for showing me this.

            thank you very much guys. it works now... you really are great...

              hi there.. i have a search engine in my site but the problem is how to split the results in multiple pages and link them with the previous page1 page2 page page4 page5 ... next buttons. plsss help....

                a year later

                I am having the same problem with the back button. What i dont understand is where i have to place this code in my app. I have a main page which checks that if session is set it takes to the page 2 otherwise to the login page.
                Now in page 2 i have logout btn alongwith other items. Now when the user logs out thru logout btn he can still go to the page 2 through back button. Please tell me where and what code should i place to avoid redirection to page2 as i am totally new to php and learning....

                I am attaching a very simple sample code, kindly have a look at it and help me
                mona

                  moona wrote:

                  I am having the same problem with the back button. What i dont understand is where i have to place this code in my app. I have user a main page which checks that if session is set it takes to the page 2 otherwise to the login page.
                  Now in page 2 i have logout btn alongwith other items. Now when the user logs out thru logout btn he can still go to the page 2 through back button. Please tell me where and what code should i place to avoid redirection to page2 as i am totally new to php and learning....

                  I am attaching a very simple sample code, kindly have a look at it and help me
                  mona

                  Then please start a new thread to avoid confusion with different answers to different people with different code.

                    Write a Reply...