<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged session</title>
<link>https://coderlegion.com/tag/session</link>
<description>Powered by Question2Answer</description>
<item>
<title>Sync Session: Rethinking Session Management in Microservices</title>
<link>https://coderlegion.com/32486/sync-session-rethinking-session-management-in-microservices</link>
<description>&lt;h2 id=&quot;sync-session-rethinking-session-management-in-microservices&quot;&gt;Sync Session: Rethinking Session Management in Microservices&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Project:&lt;/strong&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/babyblue94520/sync-session&quot;&gt;Sync Session on GitHub&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Shared storage provides the source of truth. Local caches provide performance. Events synchronize session state across service instances.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Since I started working with microservices in 2016, I have continuously explored and refined different approaches to session management.&lt;/p&gt;
&lt;p&gt;One question has remained particularly important to me:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How can every service independently verify a user's session without turning authentication into a bottleneck?&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id=&quot;the-challenge-security-vs-performance&quot;&gt;The Challenge: Security vs. Performance&lt;/h3&gt;
&lt;p&gt;In a microservices architecture, services frequently communicate with one another. From a zero-trust perspective, a service should not blindly trust a &lt;code&gt;userId&lt;/code&gt; simply because another service included it in a request.&lt;/p&gt;
&lt;p&gt;Instead, each service should validate the session and enforce the appropriate authorization rules.&lt;/p&gt;
&lt;p&gt;However, querying a shared database every time a service needs to validate a session can increase latency and database load, especially when requests pass through multiple services.&lt;/p&gt;
&lt;p&gt;This creates a practical engineering challenge: how do we preserve session validity and revocation capabilities while reducing the cost of repeated remote access?&lt;/p&gt;
&lt;p&gt;That question led me to develop &lt;strong&gt;Sync Session&lt;/strong&gt;, an open-source session management library for Spring Boot.&lt;/p&gt;
&lt;h3 id=&quot;the-design&quot;&gt;The Design&lt;/h3&gt;
&lt;p&gt;The design combines four main components:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;RDBMS as shared storage:&lt;/strong&gt; The persistent source of truth for session state.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Local cache:&lt;/strong&gt; Each service instance maintains a local copy of session data to reduce repeated database queries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Event service:&lt;/strong&gt; Session changes trigger events that allow other service instances to invalidate their corresponding local cache entries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Session ID propagation:&lt;/strong&gt; The session ID can be passed through a cookie or HTTP header, depending on the application's request flow.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The session itself is intentionally kept small. Rather than embedding a large amount of user data and permissions into the session, the application uses a protected session ID to retrieve the associated authenticated identity.&lt;/p&gt;
&lt;p&gt;User information and authorization data remain under the responsibility of the relevant systems, where they can be updated independently.&lt;/p&gt;
&lt;p&gt;This separation helps avoid relying on outdated user information stored in a session and supports centralized session revocation.&lt;/p&gt;
&lt;h3 id=&quot;what-does-this-approach-support-&quot;&gt;What Does This Approach Support?&lt;/h3&gt;
&lt;p&gt;The project aims to address several practical requirements:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Distributed session management across multiple service instances.&lt;/li&gt;
&lt;li&gt;Sliding session expiration, allowing the expiration time to be extended according to the configured policy.&lt;/li&gt;
&lt;li&gt;Administrative session revocation, enabling an administrator to force a user to log out.&lt;/li&gt;
&lt;li&gt;Synchronization of session invalidation across service instances.&lt;/li&gt;
&lt;li&gt;Reduced database access through local caching.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The key idea is that services can validate sessions locally in the common case, while shared storage and event-driven invalidation help coordinate changes.&lt;/p&gt;
&lt;h3 id=&quot;performance-reference&quot;&gt;Performance Reference&lt;/h3&gt;
&lt;p&gt;In my MySQL load tests, I measured the following results under a workload involving 100 threads and 1 million sessions:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Operation &lt;/th&gt;&lt;th&gt;Measured throughput&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Session creation &lt;/td&gt;&lt;td&gt;~15,533 operations/sec&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Local cache access &lt;/td&gt;&lt;td&gt;~20.01 million operations/sec&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Direct database queries &lt;/td&gt;&lt;td&gt;~69,278 operations/sec&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;All 1 million session creation operations completed successfully in this test, with no reported failures.&lt;/p&gt;
&lt;p&gt;Under these particular test conditions, local cache access achieved approximately &lt;strong&gt;289 times the throughput of direct database queries&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;These numbers are reference measurements from my test environment, not universal performance guarantees. The results depend on hardware, database configuration, query patterns, cache implementation, concurrency, and the exact benchmark methodology.&lt;/p&gt;
&lt;p&gt;The comparison also measures different access paths, so it should not be interpreted as an end-to-end authentication benchmark.&lt;/p&gt;
&lt;h3 id=&quot;consistency-and-security-trade-offs&quot;&gt;Consistency and Security Trade-offs&lt;/h3&gt;
&lt;p&gt;Local caching introduces an important limitation: event-driven invalidation is asynchronous.&lt;/p&gt;
&lt;p&gt;When a session is revoked, another service instance may still have a cached copy until it processes the invalidation event. Event delivery failures or temporary network partitions can extend this window.&lt;/p&gt;
&lt;p&gt;For security-sensitive systems, this behavior needs to be considered explicitly. Depending on the threat model, additional safeguards may be necessary, such as bounded cache lifetimes, reliable event delivery, recovery mechanisms, or synchronous validation for particularly sensitive operations.&lt;/p&gt;
&lt;p&gt;Likewise, validating a session is not the same as authorizing every operation. Services must still enforce their own authorization requirements, and internal service communication must be authenticated appropriately.&lt;/p&gt;
&lt;p&gt;The goal is not to claim that local caching removes every security or consistency challenge. It is to explore a practical balance between independent session validation, efficient reads, and distributed session invalidation.&lt;/p&gt;
&lt;h3 id=&quot;a-design-principle-worth-discussing&quot;&gt;A Design Principle Worth Discussing&lt;/h3&gt;
&lt;p&gt;The principle behind Sync Session can be summarized as:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Shared storage for correctness. Local cache for performance. Events for synchronization.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is not the only valid architecture, and it may not be appropriate for every workload. I am sharing it because session management is a recurring challenge in distributed systems, and I believe the trade-offs deserve discussion beyond the implementation of any single library.&lt;/p&gt;
&lt;p&gt;I'd especially like to hear from engineers who have built authentication infrastructure at scale:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;How do you handle session revocation across multiple service instances?&lt;/li&gt;
&lt;li&gt;Would you accept a short invalidation delay, or require immediate revocation for every request?&lt;/li&gt;
&lt;li&gt;How do you balance database load, cache consistency, and security requirements?&lt;/li&gt;
&lt;li&gt;What failure scenarios would you consider essential to address in this design?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I'm happy to discuss the implementation, benchmark methodology, and architectural trade-offs.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/32486/sync-session-rethinking-session-management-in-microservices</guid>
<pubDate>Mon, 26 Oct 2026 09:17:47 +0000</pubDate>
</item>
<item>
<title>Difference between session and cookies in php</title>
<link>https://coderlegion.com/27805/difference-between-session-and-cookies-in-php</link>
<description>&lt;p&gt;&lt;strong&gt;
    Today, we're going to discuss about two common things, that are &lt;em&gt;Session&lt;/em&gt; and &lt;em&gt;Cookies&lt;/em&gt;, which are used in the website to keep track of the information provided by the users, but for different purposes. A lot of beginners are confused about their characteristics, functionalities, and differences. In this study blog, we'll explore both of them in a more comprehensive way.
&lt;/strong&gt;&lt;/p&gt;
&lt;div id=&quot;toc-div&quot;&gt;
  &lt;h4&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt; Table of Contents: &lt;a id=&quot;toc-toogle&quot; class=&quot;open&quot;&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h4&gt;
  &lt;div id=&quot;toc-list&quot;&gt;
    &lt;ol&gt;
      &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774610&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Introduction to Session and Cookies&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774611&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Difference between Session and Cookies&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
      
        &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774612&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Storage Location&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774613&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Security&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774614&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Lifetime&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774615&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Stored Data Type&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774616&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Purpose of Usage&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
      
      &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774617&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;The Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774618&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;The Reference&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;section&gt;
  &lt;h2&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774610&quot; id=&quot;_Toc123774610&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Introduction to Session and Cookies&lt;/span&gt; &lt;a id=&quot;TypeError-str-object&quot; style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774610&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
  &lt;ul&gt;
    &lt;li&gt;
      &lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif; color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Session&lt;/em&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
      &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;&lt;em&gt;Session&lt;/em&gt; is used to &lt;strong&gt;store information across the multiple pages of a website momentarily on the server-side files&lt;/strong&gt;. It's useful when the website needs to track who is the current user across multiple pages of the same website. Let's look at the example:&lt;/span&gt;&lt;/p&gt;
      &lt;ol&gt;
        &lt;li&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;A user is using a POST method to send a request to the server for logging in the website.&lt;/span&gt;&lt;/li&gt;
        &lt;li&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;The information provided is sent to the server for validation via HTTP context.&lt;/span&gt;&lt;/li&gt;
        &lt;li&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;However, the HTTP context doesn't maintain the state of a user. As a result, the server is able to validate the current user at the moment only when the request is sent. The other pages are not informed about who is the current user.&lt;/span&gt;&lt;/li&gt;
        &lt;li&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;To overcome this limitation, the server will generate a unique session ID for that particular user and sent back to be stored in the browser in form of &lt;em&gt;Cookie&lt;/em&gt;.&lt;/span&gt;
        &lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Whenever the user navigates across different pages, as long as the user is showing the consistent and valid session ID, different pages may recognize the same user.&lt;/span&gt;&lt;/li&gt;
        &lt;li&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Finally, a session will end and the cookie is deleted (if expiration date is not set) when a user log out or the application is shut down.&lt;/span&gt;&lt;/li&gt;
      &lt;/ol&gt;
    &lt;/li&gt;
    &lt;li style=&quot;margin-top: 10px;&quot;&gt;
      &lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif; color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Cookies&lt;/em&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
      &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;&lt;em&gt;Cookies&lt;/em&gt; is used to &lt;strong&gt;remember the user information such as browsing history, user preferences and login credentials on the client-site computer&lt;/strong&gt;. It can only store maximum of &lt;em&gt;&lt;strong&gt;4KB&lt;/strong&gt;&lt;/em&gt; data in the browser. The server will forget everything about the user such as his/her credentials and activities after the connection breaks. Hence, &lt;em&gt;Cookies&lt;/em&gt; are very useful when you want a browser to &quot;remember&quot; the user. Let's look at the example:&lt;/span&gt;&lt;/p&gt;
      &lt;ol&gt;
        &lt;li&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;A user, called &quot;Alex&quot; has chosen &lt;strong&gt;Keep me signed in&lt;/strong&gt; and logged in to a website, his login credentials are stored in a &lt;em&gt;Cookies&lt;/em&gt; and the server creates a session ID for him and sent back to the browser.&lt;/span&gt;&lt;/li&gt;
        &lt;li&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;While navigating across multiple pages of the website, he's able to use his unique session ID to verify he's the current user.&lt;/span&gt;&lt;/li&gt;
        &lt;li&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;After a few moment, he feels tired and close the browser.&lt;/span&gt;&lt;/li&gt;
        &lt;li&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;When he comes back again and open the website, the browser will send a request to the server to get the web page, the &lt;em&gt;Cookies&lt;/em&gt; belongings to that page will be added to the request via HTTP headers.&lt;/span&gt;
        &lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Since &lt;em&gt;Cookies&lt;/em&gt; remembers his login credentials, it'll check the informtion against the server&lt;/span&gt;, and thus log in &quot;Alex&quot; automatically.&lt;/li&gt;
        &lt;li&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;The lifetime of &lt;em&gt;Cookies&lt;/em&gt; can be set and it's deleted when its lifetime has passed.&lt;/span&gt;.&lt;/li&gt;
      &lt;/ol&gt;
      &lt;div style=&quot;margin-top: 15px&quot; class=&quot;div-purple&quot;&gt;
        &lt;span class=&quot;alert-header&quot;&gt;Note&lt;/span&gt;
        &lt;span class=&quot;alert-body&quot;&gt;Since &lt;em&gt;Cookies&lt;/em&gt; are stored on client-site computer, it can only be accessed by the user that initially created it. Other user cannot see it.&lt;/span&gt;
      &lt;/div&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Now, we've some basic understanding on what is &lt;em&gt;Session&lt;/em&gt; and &lt;em&gt;Cookies&lt;/em&gt;. Let's go through their differences.&lt;/span&gt;&lt;/p&gt;
&lt;/section&gt;
&lt;section&gt;
  &lt;h2&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774611&quot; id=&quot;_Toc123774611&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Difference between Session and Cookies&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774611&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Although both are used to track the information provided by the users, they still have some distinct differences. Let's see one by one:&lt;/span&gt;&lt;/p&gt;
  &lt;h3&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774612&quot; id=&quot;_Toc123774612&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Storage Location&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774612&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h3&gt;
  &lt;ul&gt;
    &lt;li&gt;
      &lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif; color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Session&lt;/em&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
      &lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;A &lt;em&gt;Session&lt;/em&gt; is stored as server-site files temporarily on the server. It'll be deleted when the application is shut down or the user logged out.&lt;/span&gt;
    &lt;/li&gt;
    &lt;li style=&quot;margin-top: 10px;&quot;&gt;
      &lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif; color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Cookies&lt;/em&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
      &lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;A &lt;em&gt;Cookies&lt;/em&gt; is stored as a client-site files on the user's computer. It'll be deleted when the expiration date has reached.&lt;/span&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;h3&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774613&quot; id=&quot;_Toc123774613&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Security&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774613&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h3&gt;
  &lt;ul&gt;
    &lt;li&gt;
      &lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif; color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Session&lt;/em&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
      &lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;A &lt;em&gt;Session&lt;/em&gt; is more secure compare to a &lt;em&gt;Cookies&lt;/em&gt; because it's on server-site and cannot be tampered easily. Besides, the data in a &lt;em&gt;Session&lt;/em&gt; is encrypted and converted to binary form in the server.&lt;/span&gt;
    &lt;/li&gt;
    &lt;li style=&quot;margin-top: 10px;&quot;&gt;
      &lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif; color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Cookies&lt;/em&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
      &lt;span style=&quot;font-family: arial, helvetica, sans-serif&quot;&gt;A &lt;em&gt;Cookies&lt;/em&gt; is less secure because it's stored in the form of &lt;strong&gt;string text file&lt;/strong&gt; on the client's computer. Whenever an unauthorized user get access to the system, he can alter the data easily.&lt;/span&gt;
      &lt;div style=&quot;margin-top: 15px&quot; class=&quot;div-pink&quot;&gt;
        &lt;span class=&quot;alert-header&quot;&gt;Caution&lt;/span&gt;
        &lt;span class=&quot;alert-body&quot;&gt;When you choose &quot;&lt;strong&gt;&lt;em&gt;Keep me logged in&lt;/em&gt;&lt;/strong&gt;&quot;, if someone has stolen the unique identifier in the cookies, it's called &lt;em&gt;cookie stealing&lt;/em&gt; and now they can access your account. Always be aware of this option! &lt;/span&gt;
      &lt;/div&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;h3&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774614&quot; id=&quot;_Toc123774614&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Lifetime&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774614&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h3&gt;
  &lt;ul&gt;
    &lt;li&gt;
      &lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif; color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Session&lt;/em&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
      &lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;A &lt;em&gt;Session&lt;/em&gt; will be deleted once the user logs out or the application is shut down.&lt;/span&gt;
    &lt;/li&gt;
    &lt;li style=&quot;margin-top: 10px;&quot;&gt;
      &lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif; color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Cookies&lt;/em&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
      &lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;A &lt;em&gt;Cookies&lt;/em&gt; is deleted when the user specified expiration date has passed.&lt;/span&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;h3&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774615&quot; id=&quot;_Toc123774615&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Stored Data Type&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774615&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h3&gt;
  &lt;ul&gt;
    &lt;li&gt;
      &lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif; color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Session&lt;/em&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
      &lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;A &lt;em&gt;Session&lt;/em&gt; stores the any type of value or the data type of an Object.&lt;/span&gt;
    &lt;/li&gt;
    &lt;li style=&quot;margin-top: 10px;&quot;&gt;
      &lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif; color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Cookies&lt;/em&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
      &lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;A &lt;em&gt;Cookies&lt;/em&gt; can only stores data in &quot;string&quot; type only.&lt;/span&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;h3&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774616&quot; id=&quot;_Toc123774616&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Purpose of Usage&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774616&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h3&gt;
  &lt;ul&gt;
    &lt;li&gt;
      &lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif; color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Session&lt;/em&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
      &lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
        A &lt;em&gt;Session&lt;/em&gt; is used to track an user's activity over a period of time. It's used to store the specific information of a user which should be consistent across multiple pages of a website. The example can be &lt;em&gt;login status&lt;/em&gt;, &lt;em&gt;user preferences&lt;/em&gt;, and &lt;em&gt;shopping cart&lt;/em&gt;. When a user logs in the website, a session is generated in the server-site and the unique session ID is generated and stored in the user's devices as a cookie.
      &lt;/span&gt;
    &lt;/li&gt;
    &lt;li style=&quot;margin-top: 10px;&quot;&gt;
      &lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif; color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Cookies&lt;/em&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
      &lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
        A &lt;em&gt;Cookies&lt;/em&gt; are used to store user's information in their computer or mobile device to &lt;strong&gt;&quot;remember&quot;&lt;/strong&gt; their information such as tracking &lt;em&gt;login information&lt;/em&gt;, &lt;em&gt;user behavior&lt;/em&gt; and storing &lt;em&gt;user preferences&lt;/em&gt;. When the next time they visit the same website, the browser'll send the &lt;em&gt;Cookies&lt;/em&gt; along with the request to get the web page which compromises of more personalized and desired data for that particular user.&lt;/span&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  

  &lt;section&gt;
    &lt;h2&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774617&quot; id=&quot;_Toc123774617&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;The Conclusion&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774617&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
    &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;In this study blog, we've learned what's &lt;em&gt;Session&lt;/em&gt; and &lt;em&gt;Cookies&lt;/em&gt;, what are their differences in term of &lt;em&gt;storage location&lt;/em&gt;, &lt;em&gt;security&lt;/em&gt;, &lt;em&gt;lifetime&lt;/em&gt;, &lt;em&gt;stored data type&lt;/em&gt;, and &lt;em&gt;purpose of usage&lt;/em&gt;. Bear in mind, both of them have their own pros and cons, but &lt;em&gt;Session&lt;/em&gt; is more secure compared to &lt;em&gt;Cookies&lt;/em&gt;. Hence, you should choose which one to use according to your own condition.&lt;/span&gt;&lt;/p&gt;
  &lt;/section&gt;

  &lt;section&gt;
    &lt;h2&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774618&quot; id=&quot;_Toc123774618&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;The Reference&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774618&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
    &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
        You may go through this reference for more information:
      &lt;/span&gt;&lt;/p&gt;
    &lt;ol&gt;
      &lt;li&gt;&lt;em&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;What's Session and Cookies: &lt;/span&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.oracle.com/en/middleware/webcenter/sites/12.2.1.3/wbcsd/sessions-and-cookies1.html#GUID-F8F240F3-E769-4B8C-8B7C-3886010D27BE&quot; target=&quot;_blank&quot;&gt;https://docs.oracle.com/en/middleware/webcenter/sites/12.2.1.3/wbcsd/sessions-and-cookies1.html#GUID-F8F240F3-E769-4B8C-8B7C-3886010D27BE&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/section&gt;&lt;/section&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/27805/difference-between-session-and-cookies-in-php</guid>
<pubDate>Sun, 20 Sep 2026 07:36:12 +0000</pubDate>
</item>
<item>
<title>#  How Authentication Really Works (JWT vs Session vs OAuth)</title>
<link>https://coderlegion.com/15033/how-authentication-really-works-jwt-vs-session-vs-oauth</link>
<description>&lt;p&gt;Whenever you log in to a website…&lt;/p&gt;
&lt;p&gt;You enter:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Email&lt;/li&gt;
&lt;li&gt;Password&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And suddenly…&lt;/p&gt;
&lt;p&gt; You are “logged in”&lt;/p&gt;
&lt;p&gt;But what actually happens behind the scenes?&lt;/p&gt;
&lt;p&gt;How does the system remember you?&lt;/p&gt;
&lt;p&gt;Let’s break it down simply &lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;first-what-is-authentication-&quot;&gt;First: What is Authentication?&lt;/h3&gt;
&lt;p&gt;Authentication =&lt;/p&gt;
&lt;p&gt; “Proving that you are who you say you are”&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Login form&lt;/li&gt;
&lt;li&gt;OTP verification&lt;/li&gt;
&lt;li&gt;Google login&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-basic-flow-before-jwt-session-oauth-&quot;&gt;⚙️ Basic Flow (Before JWT / Session / OAuth)&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;User enters email + password&lt;/li&gt;
&lt;li&gt;Server verifies credentials&lt;/li&gt;
&lt;li&gt;Now server needs to “remember” the user&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt; This is where things get interesting&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;1-session-based-authentication&quot;&gt;1. Session-Based Authentication&lt;/h2&gt;
&lt;h3 id=&quot;how-it-works&quot;&gt;How It Works&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;User logs in&lt;/li&gt;
&lt;li&gt;Server creates a &lt;strong&gt;session&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Stores session in database/memory&lt;/li&gt;
&lt;li&gt;Sends a &lt;strong&gt;session ID&lt;/strong&gt; to browser (via cookie)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now:&lt;/p&gt;
&lt;p&gt; Every request → sends session ID&lt;br&gt;
 Server checks → “Yes, this is the user”&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;flow&quot;&gt;Flow&lt;/h3&gt;
&lt;p&gt;Login → Session Created → Session ID stored in browser → Request with ID → Server validates&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-pros&quot;&gt;✅ Pros&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Simple to implement&lt;/li&gt;
&lt;li&gt;Easy to invalidate (logout)&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-cons&quot;&gt;❌ Cons&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Server has to store sessions&lt;/li&gt;
&lt;li&gt;Not scalable for large systems&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&quot;-2-jwt-json-web-token-&quot;&gt;⚡ 2. JWT (JSON Web Token)&lt;/h2&gt;
&lt;h3 id=&quot;how-it-works-1&quot;&gt;How It Works&lt;/h3&gt;
&lt;p&gt;Instead of storing session on server…&lt;/p&gt;
&lt;p&gt; Server gives token to user&lt;/p&gt;
&lt;p&gt;This token contains:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;User ID&lt;/li&gt;
&lt;li&gt;Expiry time&lt;/li&gt;
&lt;li&gt;Signature&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-txt&quot;&gt;header.payload.signature
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now:&lt;/p&gt;
&lt;p&gt; Client stores JWT (localStorage / cookie)&lt;br&gt;
 Sends it with every request&lt;/p&gt;
&lt;p&gt;Server:&lt;/p&gt;
&lt;p&gt; Verifies token → allows access&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;flow-1&quot;&gt;Flow&lt;/h3&gt;
&lt;p&gt;Login → JWT generated → Stored on client → Sent with requests → Verified&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-pros-1&quot;&gt;✅ Pros&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Stateless (no server storage)&lt;/li&gt;
&lt;li&gt;Scalable&lt;/li&gt;
&lt;li&gt;Fast&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-cons-1&quot;&gt;❌ Cons&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Hard to revoke instantly&lt;/li&gt;
&lt;li&gt;Needs proper security handling&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&quot;3-oauth-login-with-google-github-&quot;&gt;3. OAuth (Login with Google, GitHub)&lt;/h2&gt;
&lt;h3 id=&quot;how-it-works-2&quot;&gt;How It Works&lt;/h3&gt;
&lt;p&gt;Instead of creating your own login system…&lt;/p&gt;
&lt;p&gt; You use another provider&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;“Login with Google”&lt;/li&gt;
&lt;li&gt;“Login with GitHub”&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&quot;flow-2&quot;&gt;Flow&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;User clicks Google login&lt;/li&gt;
&lt;li&gt;Redirect to Google&lt;/li&gt;
&lt;li&gt;User authenticates&lt;/li&gt;
&lt;li&gt;Google sends token back&lt;/li&gt;
&lt;li&gt;Your app trusts Google → logs user in&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-pros-2&quot;&gt;✅ Pros&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;No need to manage passwords&lt;/li&gt;
&lt;li&gt;More secure&lt;/li&gt;
&lt;li&gt;Faster user onboarding&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-cons-2&quot;&gt;❌ Cons&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Dependency on third-party&lt;/li&gt;
&lt;li&gt;Slightly complex setup&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&quot;jwt-vs-session-vs-oauth-simple-comparison-&quot;&gt;JWT vs Session vs OAuth (Simple Comparison)&lt;/h2&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Feature     &lt;/th&gt;&lt;th&gt;Session    &lt;/th&gt;&lt;th&gt;JWT         &lt;/th&gt;&lt;th&gt;OAuth&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Storage     &lt;/td&gt;&lt;td&gt;Server     &lt;/td&gt;&lt;td&gt;Client      &lt;/td&gt;&lt;td&gt;Third-party&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Scalability &lt;/td&gt;&lt;td&gt;Medium     &lt;/td&gt;&lt;td&gt;High        &lt;/td&gt;&lt;td&gt;High&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Complexity  &lt;/td&gt;&lt;td&gt;Easy       &lt;/td&gt;&lt;td&gt;Medium      &lt;/td&gt;&lt;td&gt;Medium-Hard&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Use Case    &lt;/td&gt;&lt;td&gt;Small apps &lt;/td&gt;&lt;td&gt;APIs / SPAs &lt;/td&gt;&lt;td&gt;Social login&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h3 id=&quot;when-to-use-what-&quot;&gt;When to Use What?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Small app → Session&lt;/li&gt;
&lt;li&gt;Modern API / frontend → JWT&lt;/li&gt;
&lt;li&gt;Social login → OAuth&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt; Many apps use combination (JWT + OAuth)&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;-common-mistakes&quot;&gt;⚠️ Common Mistakes&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Storing JWT insecurely ❌&lt;/li&gt;
&lt;li&gt;Not setting expiry ❌&lt;/li&gt;
&lt;li&gt;Weak session management ❌&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt; Security matters a lot here&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;final-thought&quot;&gt;Final Thought&lt;/h3&gt;
&lt;p&gt;Authentication looks simple on UI…&lt;/p&gt;
&lt;p&gt;But behind the scenes:&lt;/p&gt;
&lt;p&gt; It’s all about trust, identity, and security&lt;/p&gt;
&lt;p&gt;As a developer:&lt;/p&gt;
&lt;p&gt;Understanding this = real backend skill&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Now next time you click “Login”…&lt;/p&gt;
&lt;p&gt;You know exactly what’s happening &lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/15033/how-authentication-really-works-jwt-vs-session-vs-oauth</guid>
<pubDate>Fri, 17 Apr 2026 06:14:22 +0000</pubDate>
</item>
</channel>
</rss>