When using sessions, PHP sets the session id for you. It’s usually a 32 character string. You can get that value by calling this function after session_start(). Calling it before you start a session will result in an empty string.

You can also use this function to set your own session id. Just give it the string you want to use.

If you set the id after starting a session, that id will not persist through to the next session creation. In order to have that happen, set the id before starting a session.

Changing the id with session_id() after starting a session:

session_start();
session_id( 'happy' );
$id = session_id(); // happy
 
session_destroy();
session_start();
 
$id = session_id(); // 3ca805893ee0155fc168ce975e6f15d5

Changing the id before starting a session:

session_id( 'happy' );
session_start();
$id = session_id(); // happy
 
session_destroy();
session_start();
 
$id = session_id(); // happy