Can You Switch PHP Sessions In a Session?

前端 未结 7 1779
情深已故
情深已故 2020-12-09 19:31

I have two apps that I\'m trying to unify. One was written by me and another is a CMS I am using. My authentication happens in the one I coded and I\'d like my CMS to know t

7条回答
  •  忘掉有多难
    2020-12-09 20:25

    I've been working on perfecting this and here is what I've come up with. I switch to a parent session using session names in my child apps and then back to my child app's session. The solution creates the parent session if it does not exist.

    $current_session_id = session_id();
    $current_session_name = session_name();
    
    session_write_close();
    
    $parent_session_name = 'NameOfParentSession';
    
    // Does parent session exist?   
    if (isset($_COOKIE[$parent_session_name])) {
    
        session_id($_COOKIE[$parent_session_name]);
        session_name($parent_session_name);
        session_start();
    
    } else {
        session_name($parent_session_name);
        session_start();
    
        $success = session_regenerate_id(true);
    }
    
    $parent_session_id = session_id();
    
    // Do some stuff with the parent $_SESSION 
    
    // Switch back to app's session
    session_write_close();
    session_id($current_session_id);
    session_name($current_session_name);
    session_start();
    

提交回复
热议问题