CodeIgniter Forums
Session don't close - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Session don't close (/showthread.php?tid=43890)



Session don't close - El Forum - 07-27-2011

[eluser]bhenbe[/eluser]
Hi all,

i have a little frustrating problem since two days... I had a disconnect button to my website and it don't works.

All is in the same controller : myaccount.

For each load, some global view are called. Account-box is a view with the "hello" and somes account infos...
Code:
class Myaccount extends CI_Controller {
    
    private $datas = array();
    
    public function __construct() {
        
        parent::__construct();
        
        $datas = array();
        
        /*** session ***/
        
        $datas['account'] = $this->active_session->get_account($this->session->userdata('id'));
            
        /*** end session ***/
        
        /*** start global views ***/

        $this->datas['top_view'] = $this->load->view('global/top', $datas, true);
        $this->datas['top_menu_view'] = $this->load->view('global/top-menu', $datas, true);
        $this->datas['account_box_view'] = $this->load->view('global/account-box', $datas, true);
        $this->datas['footer_view'] = $this->load->view('global/footer', $datas, true);
        
        /*** end global views ***/
        
    }

Here's the librarie Active_session called in the previous code :
Code:
public function get_account($user_id = false) {
        
        $CI =& get_instance();
        
        if ($user_id == false)
            return false;
            
           $CI->load->model('account_model');

        $return = $CI->account_model->get_by_id($user_id);
        
        if ($return == false)
            return false;
            
        $return['notification_count'] = $this->get_notification_count($user_id);
        $return['notifications'] = $this->get_notifications($user_id);
        
        return $return;

    }

The open session code (redirect to the homepage if ok) :

Code:
public function identify() {

        $this->load->library('form_validation');
        
        $this->form_validation->set_error_delimiters('<div class="form-error">', '</div>');
            
        $this->form_validation->set_rules('account-email', 'Adresse e-mail', 'trim|required|valid_email|callback_mail_exists|xss_clean');
        $this->form_validation->set_rules('account-password', 'Mot de passe', 'trim|required|min_length[6]|max_length[20]|alpha_num|callback_password_exists|xss_clean');
            
        if ($this->input->post('check-form') == 'true') {
        
            if ($this->form_validation->run() == true) {
            
                $this->load->model('account_model');
                
                try {
                    
                    $account = $this->account_model->get_by_email($this->input->post('account-email'));
                    
                    $this->session->set_userdata('id', $account['id']);
                    $this->session->set_userdata('rank', $account['rank']);
                    
                    redirect('', 'refresh');
                    
                    return;
                    
                } catch (Exception $e) {
                    
                    $this->load->library('custom_errors');
                    
                    $this->custom_errors->set_error($e);
                    
                    return;
                    
                }
            
            }
            
        }
      
        $this->load->view('myaccount/identify', $this->datas);
        
    }

The close session code :
Code:
public function disconnect() {
        
        $this->session->unset_userdata('id');
        $this->session->unset_userdata('rank');
        $this->session->sess_destroy();
        
        redirect('', 'refresh');
        
        return;
        
    }

And, another thing perhaps usefull... I write my code in english but the url's must be in french. So, i have some routes :
Code:
$route['mon-compte/identifier'] = 'myaccount/identify';
$route['mon-compte/deconnecter'] = 'myaccount/disconnect';

The disconnect function don't seems to work... Thanks for your help and sorry for my english.


Session don't close - El Forum - 07-28-2011

[eluser]bhenbe[/eluser]
Little up. Nobody have an idea ?


Session don't close - El Forum - 07-29-2011

[eluser]bhenbe[/eluser]
I do some other test and here's the results : i try to unset the userdata vars and nothing happen so i try to change the values of the userdata created before and no changes.

Here's the config :

Code:
$config['sess_cookie_name']        = 'csj_session';
$config['sess_expiration']        = 7200;
$config['sess_expire_on_close']    = FALSE;
$config['sess_encrypt_cookie']    = FALSE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']        = 'session';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent']    = TRUE;
$config['sess_time_to_update']    = 300;

I don't understand why the CI session can read the values but can't change and destroy them. I read in the forum that the sess_destroy function doesn't work but never read that we can't modifiy the datas.

Thanks for your help.


Session don't close - El Forum - 07-29-2011

[eluser]Vlad Balmos[/eluser]
It's a wild guess but here it goes. CI Sessions are stored in plain cookies, and not the regular server side php sessions. If your site is hosted at www.mysite.com, but you can access the site with or without the [www.], like: mysite.com and www.mysite.com, you will actually set 2 session cookies. One cookie for mysite.com and the other one for www.mysite.com.
If you're browsing www.mysite.com then switch to mysite.com and then disconnect from www.mysite.com you will delete only the session for www.mysite.com, the other one still exists. So maybe there's a conflict there.

Check the cookies using Firebug and Firecookie for Firefox.

Hope it helps and i didn't give you a headache Smile


Session don't close - El Forum - 07-29-2011

[eluser]bhenbe[/eluser]
Hi,

thanks for your help. I Check this issue but it's not the problem.

I find what's wrong but it's still a problem... I used routes to redirect to the good controller and it seems that the redirect works perfectly but all the code before the redirect is not used. I don't know if it's a CI priority problem or another thing.

So, if i use CI like that, no problem :

Code:
Controller myaccount -> function disconnect { session close }

Navigator : http://www.mysite.com/myaccount/disconnect = OK, disconnected

But not working :

Code:
Controller myaccount -> function disconnect { session close }

Config Route -> $routes['moncompte/deconnexion'] = 'myaccount/disconnect';

Navigator : http://www.mysite.com/moncompte/deconnexion = OK for redirect and view, not disconnected



Session don't close - El Forum - 07-29-2011

[eluser]Vlad Balmos[/eluser]
can't figure it out. The routing feature shouldn't interfere with session management... Maybe someone else here has some better ideeas. Good luck!


Session don't close - El Forum - 07-29-2011

[eluser]bhenbe[/eluser]
I don't understand too...

I make a lot of tests and, separatly, everything works fine. Route + Session + Redirect seems to be the problem... Don't know why...