Welcome Guest, Not a member yet? Register   Sign In
Logout and flashdata
#1

[eluser]victorche[/eluser]
Please, give me a hint about this case ... I've read all the user guide and I think I am not making a mistake, but anyway it is not working. Here is the problem.

My login is in the header, which means accesible trough all the site areas. After successful login, the header is changed and there is of course a logout link.

The code logic is like this ... I have My_Controller.php, which takes care for the login stuff.
Code:
class MY_Controller extends Controller {

    function MY_Controller () {
        parent::Controller();

        // Putting all the login stuff here ...
        if ( ! $this->auth->logged_in())
        {
                // Set some form rules ...
                $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
                $this->form_validation->set_rules('password', 'Password', 'required');

                if ($this->form_validation->run() == true)
                {
                          // Making checks if the user is logging in successfully or not and setting some flashdata with info and error messages ...
                          $this->session->set_flashdata('message', $this->auth->errors());
                }
                // Let's display the messages ...
                $messages[] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message'); // This should catch the logout message too, but it is not :(
                $this->message->set($messages);
        }
        else
        {
             // The user is already logged in ...
        }

    }

}
I am also making a logout.php (controller), as my logout link is site.com/logout:
Code:
class Logout extends MY_Controller {

        function  __construct() {
              parent::Controller();
        }

        function index() {
              $this->auth->logout();
              $this->session->set_flashdata('message', $this->auth->messages()); // Here the logout message is set ...
              // echo $this->session->flashdata('message'); // If we echo here, the message is displayed

              // And this is the redirect, after which the message should be catched and displayed. But it is not :(
              redirect($this->config->item('base_url'), 'refresh');
        }

}
So, the explanation is in the comments. Any ideas why the logout message is not displayed?

Thanks in advance!
#2

[eluser]WanWizard[/eluser]
Does your logout method destroy the session?
#3

[eluser]Bainzy[/eluser]
have you echo'd the flashdata on your base_url page ??
#4

[eluser]victorche[/eluser]
@WanWizard, yes... It does. But anyway the flashdata is set after the logout method. If I echo the flashdata in logout.php (as with the commented line in the example), it is displayed. It is not displayed on the index, after refresh. This is the problem. And it should, according to all the examples and the user guide.

@Bainzy, yes I tried. After refresh, on the base_url, nothing is displayed.
#5

[eluser]Bainzy[/eluser]
ahhh .. i had this problem ... the problem lies with the fact that the $this->auth->logout is destroying the session ... then your obviously setting more session data but it sounds like this is still being destroyed.

I got round this by modifying my auth library ... and in the logout function after the sess destroy code then set the flash data message ... then im not 100% but you may need to set $this->session->keep_flashdata('message'); in your controllers logout function.

Give it a try both ways and see if it works ... if not please post up your logout function from your auth library so i can see whats going on Smile
#6

[eluser]WanWizard[/eluser]
My point exactly.

Destroying a session is (almost) never a good idea. Use a properly defined structure, and delete only that.

For ExiteCMS, the session contains a variable called 'auth', which is an array with all kind of authentication related information. If this variable exists when the user requests a page, the user session is restored. If not, a guest session is started. When a user logs out, I simply delete the 'auth' variable from the session.
All other session variables are untouched, and survive login/logout actions (for example user preferences, selection or filter criteria, etc).
#7

[eluser]victorche[/eluser]
I am using ion_auth, if this will help ...
#8

[eluser]WanWizard[/eluser]
Logged-in state in Ion-auth is controlled by
Code:
$this->ci->session->userdata($identity)

So technically, all you have to do is delete this session variable, and the user will no longer be logged in. Checking the code, simply removing the sess_destroy() call in the logout() method should be sufficient, it unsets all session variables used by Ion-auth.
#9

[eluser]victorche[/eluser]
@WanWizard, thank you but can you be a little bil more specific? I mean ... I really could not understand the idea.
#10

[eluser]WanWizard[/eluser]
In the file ion_auth_model.php (in your application/model folder), there is a method called logout().
This method unsets all session variables created by Ion-auth, removes the cookies, and destroys the session. Imho the last thing is not strictly needed.

So open the file, look for the method, and comment the line that says
Code:
$this->session->sess_destroy();

This should solve the issue, without impact to Ion-auth's functionality.




Theme © iAndrew 2016 - Forum software by © MyBB