CodeIgniter Forums

Full Version: Two Separate Sessions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]MisaghCJ[/eluser]
Hi everyone,

I have been searching for my problem since yesterday morning but I couldn't find any solution.

I want to Set two or three or some separate sessions with different names.

for example =>
one sessions : session_1
another session : session_2
another : session_3
......

all of them have different data.

I have found this code :

Code:
$config_session1 = array(
            'sess_cookie_name' => 'session1',
            'sess_expiration' => 3600
        );
        $this->load->library('session', $config_session1, 'session1');
        // Do something with data of session1
        $this->session1->set_userdata('sess1_data', 'salam');
        echo $this->session1->userdata('sess1_data').'<br>';

        // Create session2
        $config_session2 = array(
            'sess_cookie_name' => 'session2',
            'sess_expiration' => 7200
        );
        $this->load->library('session', $config_session2, 'session2');
        // Do something with session2
        $this->session2->set_userdata('sess2_data', 'salam2');
        echo $this->session2->userdata('sess2_data');

But it won't work in my project and I have tested any ways but it won't...

Please help me.

Thanks all.

El Forum

[eluser]InsiteFX[/eluser]
And why would you want to do that?

InsiteFX

El Forum

[eluser]MisaghCJ[/eluser]
Hi,

Thanks a lot for you reply.

Because I have administrator section for website and another section for user login in main section.

after that I want to keep some data in cookie that is for contact form. It is completely different data than two others.

I think if I use one session for both admin and users, security goes down and If I am wrong please tell me.

For example I want to keep the admin session for only 2 hours (expire in 2 hours) but the users session for 6 months.

El Forum

[eluser]InsiteFX[/eluser]
If you login as an admin then it is an admin session otherwise it is a user session

There is only one session but it depends on who is logged in!

You just do a check if is_admin then do this else do that!

InsiteFX

El Forum

[eluser]MisaghCJ[/eluser]
(( Thanks for your reply ))

Yes, I know this.

But my problem is here :

I want to set admin session for only 2 hours and users session for 6 months.

I think I can set this just with 2 different sessions.

What do you think?

El Forum

[eluser]InsiteFX[/eluser]
Then you need a remember me cookie for the user and a regular cookie for admin

Like I said it depends on who is logged in! if is_admin setcookie 2 hours else set user cookie 6 months

InsiteFX

El Forum

[eluser]InsiteFX[/eluser]
Something like this:
Code:
// delete the current cookie
setcookie("logged_in", $identifier, time() - 60*60*24*30, '/');

$admin = $this->_ci->session->userdata('admin');

// time() + 60*60*24*30 will set the cookie to expire in 30 days
if ($admin == '1')
{
    // Admins cookie 2 hours
    setcookie("logged_in", $identifier, time() + 7200, '/');
}
else
{
    // Users cookie 6 months
    setcookie("logged_in", $identifier, time() + 60*60*24*180, '/');
}

InsiteFX

El Forum

[eluser]MisaghCJ[/eluser]
Thank you very very very much!

for this project I can use this surely.

But generally I want to know If there is a way to set 2 different sessions in Codeigniter or not?

El Forum

[eluser]InsiteFX[/eluser]
As far as I know you can only have one session at a time even with native PHP, but I could be wrong.

InsiteFX

El Forum

[eluser]tonanbarbarian[/eluser]
just a thought but why not store multiple arrays of data in the session and have your own timeout code that you check in each case as needed?
one array contains the admin session information and the timeout
another array contains the user session information and timeout.

you just need to have code that checks if the user is past the timeout rather than letting the session handle that.
Pages: 1 2