开发者

How to use two sessions for one user in codeigniter?

开发者 https://www.devze.com 2023-01-02 17:40 出处:网络
I am using the cart class for a shoping cart. Now I want to use the Simplelogin library, but the cart session is erased when I login.

I am using the cart class for a shoping cart.

Now I want to use the Simplelogin library, but the cart session is erased when I login.

How can this be solved ?

It is possible to use two 开发者_JAVA百科sessions for one user ?

Or maybe merge all in the same session ?


I don't know if this will help you, since I don't use Code Igniter and don't know how the stuff is handled there.

But I usually use sub-arrays for everything I do in sessions, so no vars are in danger of being overwritten. So if I have a login-script it would only save to $_SESSION['login'] (for example $_SESSION['login']['password_hash'])

If that's not possible with code igniter or that library always deletes the session (what kind of library would do something like that???) you have to save the data somewhere else. Either in a hidden form field in the login-form (don't forget to serialize the data first) or in a Cookie.


Simple login follows a very common procedure, recreating a new session after loggin, so cart data must be saved just before user logs in.

you can see that in Simplelogin's function login code:

     //Destroy old session
    $this->CI->session->sess_destroy();

    //Create a fresh, brand new session
    $this->CI->session->sess_create();

So in your login controller, where you call simplelogin->login($user, $password) you must save before login and restore your cart later, something like this:

$savedCart = $this->cart->contents();
if ( $this->simplelogin->login($user, $password) ){
   // at this point the session has been regenerated, so 
   $this->cart->insert($savedCart); 
}

hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消