How to get session_id in PHP?

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

How to get session_id in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by darion , 3 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 , 2 years 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.

Related Threads:

How to restore session by session_id in codeigniter?
How to redirect after 5 seconds in PHP?
How to get project or root dir in symfony 5?
How to get headers in PHP?
How get yesterday date in PHP?
How get current year in PHP?