How to get session_id in PHP?

by hal.littel , in category: PHP General , 2 years ago

How to get session_id in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by darion , 2 years ago

@hal.littel you can use session_id() function to get session_id in PHP, code:


1
2
3
4
5
6
7
8
9
<?php

// Start session
session_start();

// Get session id
$id = session_id();

echo $id;


Member

by lottie , a year ago

@hal.littel 

In PHP, you can get the session ID using the session_id() function. This function returns the current session ID or an empty string if no sessions are active.


Here's an example:

1
2
3
4
5
6
7
8
// Start the session
session_start();

// Get the session ID
$session_id = session_id();

// Print the session ID
echo "Session ID: " . $session_id;


Make sure to call session_start() before using session_id() to initialize the session and generate a session ID if none exists.