Skip to content

[zend-session] fix php 8.4+ deprecation when setting a save handler - #245

Open
falkenhawk wants to merge 1 commit into
masterfrom
fix-session-save-handler
Open

falkenhawk wants to merge 1 commit into
masterfrom
fix-session-save-handler

Conversation

@falkenhawk

Copy link
Copy Markdown
Member

1) php 8.4: deprecation when a save handler is set

Zend_Session::setSaveHandler() passes the six methods of the save handler to session_set_save_handler() one by one.
php 8.4 deprecated that, so every app using a save handler, e.g. Zend_Session_SaveHandler_DbTable, gets this on every request:

Deprecated: session_set_save_handler(): Providing individual callbacks instead of an object implementing SessionHandlerInterface is deprecated

On php 8.4+ the save handler is now wrapped in a new Zend_Session_SaveHandler_SessionHandlerAdapter, which implements SessionHandlerInterface.
Nothing changes on older php versions.

The adapter makes php treat the save handler exactly as before:

  • calls and return values are passed through unchanged
  • it is registered with $register_shutdown = false, because the old way did not close the session on shutdown either
  • php also picks up create_sid(), validateId() and updateTimestamp() methods of the registered object. The adapter is registered instead of the save handler, so such methods of a save handler stay unused, as before.

2) php 8.6: create_sid() and validateId()

php 8.6 warns about classes implementing SessionHandlerInterface without create_sid() and validateId(), php 9 will require them (rfc).
The adapter has both, and they behave like php's built-in defaults for the old way:

  • create_sid() creates the session id with php's default generator (session_create_id())
  • validateId() accepts every session id, so session.use_strict_mode has no effect, as before
  • except for a session id it has just created: php asks validateId() whether such an id already exists, to avoid collisions in session_regenerate_id() and session_create_id(). php skipped this check for the old way, so the adapter answers "no". Otherwise session_create_id() would fail during an active session.

3) Session tests

  • DbTableTest::testSessionSaving ran in unit test mode, so the save handler was never registered and the test passed without asserting anything. It now uses a real session, and it fails without the fix on php 8.4+.
  • the new SessionHandlerAdapterTest checks the pass-through and the session id handling. The session id tests run a real session in a separate process, with the same assertions on all php versions: the old way below php 8.4, the adapter from php 8.4 on.
  • Zend_SessionTest::testSetExpireSessionHops and testSetExpireSessionVarsByHopsOnUse failed on php 8.1+. The helper script prints nothing once the session namespace has expired, and explode() got null from array_pop() on the empty output. Same fix as diablomedia/zf1-session#145.

Other forks

  • zf1-future, Shardj/zf1-future#539 and #545: also an adapter, but used on all php versions. It changes return values (casts to bool, a null from read() becomes false), still closes the session on shutdown, and has no create_sid() and validateId() for php 8.6.
  • zfbase/zf1-future: setSaveHandler() only accepts SessionHandlerInterface objects, so existing save handlers no longer work.
  • diablomedia/zf1-session#341: Zend_Session_SaveHandler_Interface extends SessionHandlerInterface with return types, so existing save handlers have to be changed, and php 8 is required.

Ported from a private repository, based on work by @marcing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant