PHP How can I create multiple sessions?

后端 未结 2 1907
刺人心
刺人心 2020-12-03 02:02

I want to be able to switch back and forth between sessions in php. Here is my current code:



        
2条回答
  •  孤城傲影
    2020-12-03 02:32

    What you need to use is session_id() instead of session_name()

    ", print_r($_SESSION, 1), "
    "; session_write_close(); session_id("session2"); echo session_id(); session_start(); $_SESSION["name"] = "2"; echo "
    ", print_r($_SESSION, 1), "
    "; session_write_close(); session_id("session1"); echo session_id(); session_start(); echo "
    ", print_r($_SESSION, 1), "
    "; session_write_close(); session_id("session2"); echo session_id(); session_start(); echo "
    ", print_r($_SESSION, 1), "
    ";

    This will print:

    session1
    
    Array
    (
        [name] => 1
    )
    
    session2
    
    Array
    (
        [name] => 2
    )
    
    session1
    
    Array
    (
        [name] => 1
    )
    
    session2
    
    Array
    (
        [name] => 2
    )
    

    session_id is an identifier for a session, which helps in distinguishing sessions. session_name is only a named alias for the current session

提交回复
热议问题