How to get Session ID in PHP?

PHP Get Session ID | Featured 

This article demonstrates how to get a session ID in PHP. Here’s a featured snippet from the article.

<?php  
         //Starting PHP session
         session_start();  
         $id = session_id();
         print("Session Id: ".$id);
 ?>

That’s one out of two ways to get PHP session ID. Learn more about web sessions and session IDs in the following sections. 

What is a Web Session?

A web session is a set of actions a visitor takes on the same website within a specific time. This could include when we are looking up on a search engine, filling out a form to get content, scrolling down web pages, adding shopping items to the cart, or looking at pages on a single website. 

Any interaction with a website is recorded as a “web session” on that website’s property. It represents the user’s first arrival time on a web page and the time a user stopped using the website.

The following figure shows a sequence of interactions in a usual web session.

PHP get Session ID
Session Sequence Diagram

A web session never lasts beyond a specific duration. It depends on the site the user uses, and a web session may be as short as five minutes or as long as 1440 minutes (an entire day). 

What is Session ID?

Websites we visit daily use a session ID to respond to what users do during a web session.

The user’s browser stores the web session ID to track sessions. Then this session is passed with any HTTP requests a user makes when using the website, for example, clicking on a link, making a purchase, and so on.

The session ID can expire over time for security reasons like session hijacking, and backend systems grant new session IDs to active users at intervals. Think of it as a token of authorization and a unique identity in online interaction.

The Difference Between Cookies and Sessions

A cookie is a small piece of data from a specific website stored in a user’s browser. It helps the website track all the user’s activities on the website. People often confuse cookies and sessions but they are not the same things.

PHP get Session ID
Cookies vs Session

And more specifically, session IDs and cookie IDs are mixed up as they are closely related. But actually, they are not the same thing. 

A cookie is used to identify a specific user or a specific computer. It can also be used to verify the identity of a user, store preferences for a website, or store shopping carts on a website. 

A cookie can also store information such as the user’s name and preferences collected when filling out a form. The cookie then uses this information to fill in the pages the user visits during a single web session or multiple sessions.

The server logs on a website contain both a user’s session ID and cookie ID. A web session ID is unique for a specific user’s visit, while a cookie ID is unique to one particular user. So, when we map a single cookie ID to multiple session IDs, we can get a clearer picture of how the users interact with their websites.

Why do we use a Web Session?

A session is used to store data and some information in variables to be used across multiple web pages. It is different from a cookie because, in a session, the information is not stored on the user’s computer.

The main purpose of using session ID is to avoid storing massive amounts of information in a browser.

We use session IDs to keep track of the server’s information while protecting users’ privacy. When a user takes action or makes a request on the website, the website sends back the session ID and cookie ID to the server, along with a specific description of the action or request.

What is Session in PHP?

Session (also called “session handling”) is a way for a web application’s data to be available on different pages.

Let’s see how to get a session ID in PHP.

Option 1 | PHP Get Session ID using session_id() 

The function session_id() gets/sets session ID in PHP.

session_id([$id]);

PHP Get Session ID Example

Here’s a basic example of how to get a PHP session ID.

<?php  
         //Starting PHP session
         session_start();  
         $id = session_id();
         print("Session Id: ".$id);
 ?>

Output

The output is as follows. The session ID will be different for different sessions in PHP.

Session Id: b40c33c80c6e5c52a20cf9ca52df4dd4

Option 2 | PHP Get Session ID using $_SESSION[] 

$_SESSION is an associative array in PHP that contains all PHP session variables. We can add or delete a specific key value to this array or empty the entire array.

To store all the details of the authenticated users in the $_SESSION variable, we need to start a PHP session. When the session is active, we can use the session ID to track the user across the system.

PHP get Session by ID Example

We can set and get a session ID in PHP using the following code.

<?php
    // Start a new session
    session_start();

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

    // The username
    $username = "Stephen";

    // Registering a session for the user
    $_SESSION['username'] = $username;

    // Displaying the session ID
    echo "Your session id is: " . $session_id;
    echo "<br /> This session has been registered for: " . $username;
?>

Output

Here’s the output of the example above.

Your session id is: 0k7q8k1iuemonp32v62iet79le
This session has been registered for: Stephen

Perfect! That’s it for this article – time to wrap it up.

Conclusion

Wrap Up

This article demonstrates how to get a session ID in PHP. The article includes two options – session_id() and $_SESSION. Besides, it goes beyond the main subject and dives into fundamental concepts, including an introduction to web sessions, why they are important, and the difference between cookies and sessions. 

Hope you’ve liked this article. Stay tuned for more at FuelingPHP.

Classes and Functions Mentioned

session_id – (PHP 4, PHP 5, PHP 7, PHP 8) This function gets/sets session ID in PHP. It has been included in early PHP 4 and used in the latest releases.

session_start – (PHP 4, PHP 5, PHP 7, PHP 8) This function starts and resumes a session in PHP. It has been a session management function since 4th installation.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.


Article Categories

Article Tags

© 2022 Confident.Systems