PROJECTENGRAM

COOKIES AND SESSIONS

Cookies and Sessions

Tags: #◇Web Development #◇CCAPDEV #◇Cookies and Sessions #◇Code Snippets #◇M5


◇Overview

Extracted notes for Cookies and Sessions covering both theoretical and practical elements.

◇Key Definitions

COOKIES & SESSIONS
HTTP Review Remember that HTTP is a stateless protocol. This means that when you load explore a website navigating from page to page the server or even the browser has no way of tracking that its the same browser that is visiting the website. This is problematic because websites won’t be able to remember the users preferences. Browsers won't also be able to distinguish the type of user (ex. Admin, customer) that is using the website
How do we Solve this? COOKIES & SESSIONS
Cookie Properties Cookies are not secret from the user, cookies sent by the server are available to the client to look at. Users can delete cookies, users have control over cookies and browsers make it possible to delete cookies. Cookies can be tampered with, users can edit the values of the cookies Prefer session over cookies, sessions still rely on cookies but is more secure.
Cookie Properties Cookies are stored in string format Cookies live on the client side Limited amount of data can be stored at least ~4mb Cookies have a lifespan that can be set for a long time

◇Code Implementation

『』
TEXT
How cookies work? 
Post /login
201 ok; Cookie: USERID=1234
Get /posts; Cookie USERID=1234
Posts.hbs, {post data}
『』
TEXT
How to write cookies? 
app.get(“/”, function(req,res){
res.cookie(“foo”, “bar”, {
	maxAge: 60*60*1000,
	httpOnly: true
})
})
“Foo” is the name of the cookie 
“Bar” is the value of the cookie 
maxAge, expiry time relative to current time in milliseconds 
httpOnly, Flags the cookie to be only accessible by the web server, make the cookie inaccessible if used through document.cookie 
https://expressjs.com/en/5x/api.html#res.cookie
『』
TEXT
How to read cookies? 
npm install cookie-parser
const cookieparser 
= require(“cookie-parser”)
app.use(cookieparser())
app.get(“/”, function(req,res){
let cookiefoo = req.cookies.foo
if(cookiefoo){
console.log(“foo: “ + cookiefoo)
}
})
Install Cookie Parser
Import Cookie Parser
『』
TEXT
How sessions work? 
Post /login
201 ok; Cookie: SESSIONID=1234
Get /posts; Cookie SESSIONID=1234
Posts.hbs, {post data}
1234 : {
    Userid: 123,
    Usertype: “customer”
}
『』
TEXT
How to read/write sessions? 
https://github.com/expressjs/session  
app.get(“/”, function(req,res){
req.session.foo = “bar”
})
npm install express-session
app.use(session({
secret: “very super secret”,
resave: false,
saveUninitialized: true,
cookie: {
	maxAge:1000*60*60*24*30
	httpOnly: true
}
}))
const session = require(“express-session”)
Install Express Session
Import Express Session
Secret, used to sign the session ID cookie. This prevents users from being a sus
Resave, forces the session to be saved back to the session store even if  session was never modified 
SaveUninitialized, Forces a session that is uninitialized to be saved to the store
Cookie, Cookie properties are set here

◇Syntax Breakdown

Ensure correct syntax for Cookies and Sessions Pay attention to specific functions and declarations.

◇Complexity Analysis

Time Complexity: O(1) for typical operations unless looping. Space Complexity: O(1) generally.

◇References & Navigation

CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

Which of the following is a key characteristic of HTTP cookies?

Q2

In Express, when creating a cookie, what does the httpOnly: true flag do?

Q3

Why are Sessions generally preferred over plain Cookies for storing sensitive user data?