Lesson 8.4: Sessions and cookies
A session keeps one browser signed in. An administrator needs to know how long sessions last, how to see who is signed in, how to sign a browser out after an incident, and which cookie settings keep a session from being stolen. This lesson covers Setup > Sessions with both kinds of session handler and the cookie settings of chapter 13.
Note
Switching the session handler signs everybody out once. Do it only on your own training installation.
At a glance
| Course | Site administration |
| Module | Module 8: Security |
| Duration | 35 minutes (15 presenting, 20 exercise) |
| Level | Intermediate |
| Audience | Site administrators |
| Prerequisites | Lesson 8.2; an administrator account, a test user and a shell |
Learning objectives
After this lesson, learners can:
- say how long a session lasts by default and which setting changes it;
- explain why Setup > Sessions cannot list sessions with the default handler, and what it shows instead;
- switch to database sessions and remove one user's sessions through the confirmation;
- explain why removing a session is not enough to keep an attacker out;
- choose cookie settings for a site served only over HTTPS.
Trainer's notes
How to present it
Sources: Sessions: who is signed in, and signing browsers out and Chapter 13, 13.7 Sessions and cookies.
- What a session is (2 minutes). Opened at the first request that needs it; ends
site.ini [Session] SessionTimeoutseconds after its last use (31104000, 360 days, by default). - Two handlers (4 minutes).
Handler=empty (the default) means PHP keeps sessions in its own storage: the page Session administration shows who signed in recently (Recently signed in, with the last 1 h, the last day, the session lifetime and Find a user), but cannot list or remove sessions. WithHandler=ezpSessionHandlerDBsessions are in the tableezsessionand the page lists and removes them. - Removing safely (3 minutes). Remove timed out / old sessions signs nobody out. Remove all sessions signs everybody out, you included, after a confirmation. Your own current session is never removed from a selection.
- Not enough on its own (2 minutes). Removing a session signs a browser out; it does not block the user. After a
takeover, change the password (with
EndOtherSessions, lesson 8.2) or disable the account as well. - Cookies (4 minutes).
CookieSecure(auto, ortruefor HTTPS-only sites),CookieHttponly=true,CookieSameSite=Lax, and a shorterSessionTimeoutto limit how long a stolen session is useful. Separate session cookies per siteaccess keep a public sign-in from being an administration sign-in.
Common questions
Should every site switch to database sessions? Only if you need to see and remove sessions. ForceStart=enabled
also writes a row for every anonymous visit; set it only if anonymous visitors must be counted.
Users behind an HTTPS proxy are signed out on every page. Typically CookieSecure=true while the kernel thinks
the request is plain HTTP: the proxy is not trusted or does not send X-Forwarded-Proto (chapter 13, 13.10).
Why not CookieSameSite=Strict? It drops the session on the first click from an e-mail or another site into a
signed-in page, which users see as being signed out.
Pitfalls
- Signing the class out. If several learners share one installation, Remove all sessions signs everyone out. On one installation per learner it only signs out the learner.
- Forgetting the restart. On Velocity, a handler change needs a deploy so the workers read it.
- Time skew. Time skew detected in Last activity means the clocks of web servers differ or
SessionTimeoutwas lowered.
Hands-on exercise
Use your own training installation, in a terminal at the installation root and in two browsers (or a normal and a private window).
As administrator, open Setup > Sessions. Read the notice about the handler and the Recently signed in list. Try the windows the last 1 h and the session lifetime.
Read the session settings in effect:
php bin/php/console exp:ini get site.ini/Session/Handler php bin/php/console exp:ini get site.ini/Session/SessionTimeoutSwitch to database sessions:
php bin/php/console exp:ini set site.ini/Session/Handler ezpSessionHandlerDB global --dry-run php bin/php/console exp:ini set site.ini/Session/Handler ezpSessionHandlerDB global php bin/php/ezcache.php --clear-tag=iniOn Velocity, deploy as your trainer shows.
Sign in again as administrator (everyone is signed out once). In the private window, sign in as the test user.
Reload Setup > Sessions. Read the figures and find the test user's card.
Press Sessions on the test user's card to see that user's sessions one by one.
Tick the test user's session, remove it and read the confirmation before you confirm.
In the private window, reload a page: the test user is signed out.
Press Remove timed out / old sessions and read the message.
On paper, write the
[Session]settings you would use for a site served only over HTTPS, and compare them with chapter 13, section 13.7.
Expected result
- Step 1 shows the notice that sessions are kept by PHP and cannot be counted or removed there.
- Step 5 shows figures for sessions in all and signed-in users, and a card for the test user.
- Step 8: the test user's browser is signed out; the administrator's session stayed.
- Step 10 includes
CookieSecure=true,CookieHttponly=true,CookieSameSite=Laxand a chosenSessionTimeout. - The trainer checks by asking one learner to explain what else they would do after a real account takeover.

Quiz
- How long does a session last by default after its last use?
- Why can Setup > Sessions not remove sessions on a default installation?
- True or false: removing a user's sessions stops that user from signing in again.
- Which button signs nobody out? A. Remove all sessions B. Remove timed out / old sessions C. Remove selected D. All of them
- What does
CookieSecure=trueguarantee?
Answers
- 31104000 seconds, 360 days (
site.ini [Session] SessionTimeout). - The default handler keeps sessions in PHP's own storage, which the page cannot list (Sessions, section 1).
- False. It signs browsers out; disable the user or change the password as well.
- B. It removes expired rows only.
- The session cookie is never sent over plain HTTP.
Further reading
- Sessions: who is signed in, and signing browsers out: the page and its problems.
- Chapter 13: Security hardening for production: sections 13.7 and 13.10.
- TLS and HTTPS for hosts.
Related pages
- Module 8: Security
- Site administration: course overview
- Trainers: start here
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.