0

in my logout function i like to destroy all session except one here is my function :

    public function out(){

    $ref = $this->session->userdata('ref'); 
    var_dump($ref);

    $this->session->sess_destroy();

    $this->session->set_userdata('ref', $ref ); 


    $ref = $this->session->userdata('ref'); 
    echo '---------------------------------------<br />';
    var_dump($ref);


    }

but this doesnt work and it destroys all the session even ref and when i check ref in the next page i get null

inn the function page i get this output :

array (size=2)
  'val' => int 666
  'date' => int 1397060477

---------------------------------------

array (size=2)
  'val' => int 666
  'date' => int 1397060477

A PHP Error was encountered

Severity: Notice

Message: Undefined index: last_activity

Filename: drivers/Session_cookie.php

Line Number: 590

Backtrace:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: session_id

Filename: drivers/Session_cookie.php

Line Number: 611

Backtrace: 

im using 3.0-dev

max
  • 3,614
  • 9
  • 59
  • 107

4 Answers4

3

Try this:

public function out(){

    $ref = $this->session->userdata('ref'); 
    $this->session->sess_destroy(); // this kills the ID/cookie

    $this->session->sess_create(); // properly start a new session with new ID/cookie

    if($ref)        
    $this->session->set_userdata('ref', $ref ); 

     redirect(base_url().'index');  

}

Note:

sess_create() is not documented here: CodeIgniter Sessions

You have to look at /system/libraries/Session.php to find sess_create()

UPDATE

When using Dev 3.0 then you need to do this:

$this->session->__construct();
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • thanx , i've added more info .... btw i get `Call to undefined method CI_Session::sess_create() ` – max Apr 09 '14 at 16:26
  • I use CI 2.1.4 so you need to go hunting in that file I referenced. – MonkeyZeus Apr 09 '14 at 16:28
  • Holy balls it's way different now: **[CI 3.0 Session.php](https://github.com/EllisLab/CodeIgniter/blob/release/3.0/system/libraries/Session/Session.php)**. Try `$this->session->__construct();` instead of `sess_create()` – MonkeyZeus Apr 09 '14 at 16:32
  • **[~2800 commits since 2.1.1](http://lonnieezell.com/whats-new-in-codeigniter-3/)** – MonkeyZeus Apr 09 '14 at 16:34
  • thanx , i've solve it by loading sess libe again after destroying it but this works too – max Apr 09 '14 at 16:35
  • I'd have to look at the source code deeper but I am 95% sure that loading the library again is wasting resources. So definitely use `__construct()` =) – MonkeyZeus Apr 09 '14 at 16:36
  • yeah i used `construct` instead – max Apr 09 '14 at 16:40
0

thanx guys , i've solved it by loading session libe manully in the function after destroyin it ... even though i'm auto loading sess lib !

i guess they never going to solve sess lib problems !

public function out(){

$ref = $this->session->userdata('ref'); 
$this->session->sess_destroy();

$this->load->library('session');
$this->session->set_userdata('ref', $ref ); 

 redirect(base_url().'index');  

}
max
  • 3,614
  • 9
  • 59
  • 107
-1

Session is destroyed yet not recreated after destruction. Perhaps your session class should have a session_start in the constructor

morissette
  • 1,071
  • 1
  • 8
  • 29
-1

After you destroy a session, I believe it would be this line:

$this->session->sess_destroy();

You have no session at all, so you can't put anything to it.

You first store session variables in variable $ref, then you destroy whole session, and again trying to put something in session. So after destroy, you have to start another session. Something like this:

public function out(){

$ref = $this->session->userdata('ref'); 
$this->session->sess_destroy();
session_start(); // put ur session_start func here
if($ref)        
$this->session->set_userdata('ref', $ref ); 

 redirect(base_url().'index');  

}
b4rt3kk
  • 1,419
  • 3
  • 17
  • 26