How to get the current session id in PHP?

Use the session_id() function. It returns the id of the current session. Call session_start() before it, otherwise you get an empty string.

<?php
session_start();

echo "Session id: " . session_id();
?>

Output:

Sample output, your id will be different

Session id: 7f2k9d8s1l0a3q5p9c4v6b2n8m

The id is a random text and it changes for every new session. After a user logs in, create a new id with session_regenerate_id(true), it is a good security habit. See PHP Session and PHP Security Tips.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.