0
votes

I have 2 different area in my website. Site area, and Admin area. Both have different different login function to start the session and set cookies, and also logout function to delete the cookie and destroy the session.

So for example:

login:

function siteLogin(){
    ...

    session_name('siteLogin');
    session_start();
}

function adminLogin(){
    ...

    session_name('adminLogin');
    session_start();
}

logout:

function siteLogout(){
    ...

    session_name('siteLogin');
    session_start();
    session_destroy();
}

function adminLogout(){
    ...

    session_name('adminLogin');
    session_start();
    session_destroy();
}

The problem: If i'm logged in in both areas, and i logout from one, i get logged out in both.

The question: How i can prevent that? So if i logout from one, i stay logged in in the other?

1
It's usually a better idea to use just a single name for the session and to devide the $_SESSION object into "area"-specific namespaces to store data of multiple (simulated) sessions. - Niko
According to the documentation the session cookie is not deleted when you call session_destroy() (only the data). Are you manually deleting the cookie in your logout methods? - George Cummins
@GeorgeCummins I have two different cookies names, and when logging out yes, i delete the cookie name related to the area i'm logging out. - Fr0z3n
Agreed with @Niko. It's a lot easier just to use a different array key for each one rather than a whole separate session -- you're just making it more difficult for yourself by using multiple sessions. - Spudley
@Niko By namespaces you mean preceed with a variable all the session data? Like $_SESSION['site']['data'], $_SESSION['admin']['data'], and then unset($_SESSION['admin']) ? - Fr0z3n

1 Answers

1
votes

session_destroy will delete your complete session. If you want to delete particular session value use unset function, somewhat like this

unset($_SESSION['siteLogin']);