How to get/set session_id() or should it be generated automatically?

后端 未结 3 1879
生来不讨喜
生来不讨喜 2020-12-06 03:21

I have some basic session handling in my application. In each page I check if the user is logged in. If they are then they\'re already identified by $_SESSION[\'user_i

相关标签:
3条回答
  • 2020-12-06 03:43

    Well, it may be a little bit strange, but when I need unique ID I use uniqid()

    0 讨论(0)
  • 2020-12-06 03:53

    There are 2 ways to use sessions and session id's in PHP:

    1 - Auto generate the session ID and get it:

    session_start();
    $id = session_id();
    

    2 - Set the session ID manually and then start it:

    session_id( 'mySessionId' );
    session_start();
    

    If you intend to set the session ID, you must set it before calling session_start(); If you intend to generate a random session_id (or continue one already started in a previous page request) and then get that id for use elsewhere, you must call session_start() before attempting to use session_id() to retrieve the session ID.

    0 讨论(0)
  • 2020-12-06 03:53

    Here you can see it works for me (session is started silently) : http://sandbox.phpcode.eu/g/f6b6b.php

    You forgot to start your session, probably

    0 讨论(0)
提交回复
热议问题