views:

271

answers:

3

I'm not sure I explained it correctly in the question title, so here's the details:

Login controller:

function authenticate() {
    if ( authorized ) {
        redirect('lobby');
    } else {
        redirect('login');
    }
}

Lobby controller:

function index() {
    //load lobby view
}

What happens is that in the login page, I enter user / pass, click Login, then authenticate() is called, and redirects me to the Lobby.

However, when I click refresh while on the Lobby, the login form gets submitted again, so I get "logged in" again. How do I prevent that from happening?

+2  A: 

What you're seeing is behavior from the browser that's not related to CodeIgniter, per se. The web browser stores the POST data for the login form and navigates to the form's action when the user hits the submit button. The response from the server is a redirect. Therefore, when the user hits the refresh button, the browser does a refresh of the form action, not the redirect response from the server.

You've got a couple of options to work around or avoid this. The first one that comes to mind is to put a check in the authenticate method to see if the user is already logged in. If so, then go ahead and redirect to the Lobby. Otherwise, check the user's login information as normal.

You could also just change the action of the login form to go directly to the lobby and do your authentication in the Lobby's index method. If the user isn't logged in, redirect them from there back to the login form.

villecoder
A: 

http://zackhovatter.com/18/codeigniter-modular-extensions-forms/

You could also try this

Zack
A: 

You should use the controller to validate in the construct if the user is logged or not, in that way, you can redirect or not based on a session variable.

Gerardo Jaramillo