Knowledgebase

Handling Sessions and State Print

  • developersgitdeployment, developers, caching, domainrenewal, database, security, php, woocommerce, diskspace, errors
  • 0

Where user state lives.

DEFAULT BEHAVIOUR

PHP stores session data in files on the server by default, usually in your account's tmp directory.

PRACTICAL ISSUES

Session files accumulate and consume inodes. Garbage collection removes expired ones, but on shared hosting this is not always reliable. Clean the directory periodically.

Sessions do not persist across servers, which matters only if you later move to multiple servers.

ALTERNATIVES

Database-backed sessions, which are portable and queryable but add database load Redis or Memcached, where available, which is fastest Token-based state carried by the client, avoiding server-side sessions entirely

CACHING AND SESSIONS

Any page that varies by session must not be cached, or one visitor sees another's data.

This is the most serious caching error and it happens regularly. Exclude authenticated pages explicitly.

SECURITY

Set session cookies as secure and HTTP-only Regenerate the session identifier on login, preventing session fixation Set a reasonable expiry Do not put sensitive data in a cookie

DEBUGGING

Sessions lost between requests usually means the storage path is unwritable, or a caching layer is interfering.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot