Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c7a44be

Browse filesBrowse files
digilistfabpot
authored andcommitted
Use proper error message when session write fails #20807
1 parent c6e1a49 commit c7a44be
Copy full SHA for c7a44be

File tree

2 files changed

+31
-1
lines changed
Filter options

2 files changed

+31
-1
lines changed

‎src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php
+23-1Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpFoundation\Session\Storage;
1313

14+
use Symfony\Component\Debug\Exception\ContextErrorException;
1415
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
1516
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSessionHandler;
1617
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
@@ -208,7 +209,28 @@ public function regenerate($destroy = false, $lifetime = null)
208209
*/
209210
public function save()
210211
{
211-
session_write_close();
212+
// Register custom error handler to catch a possible failure warning during session write
213+
set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) {
214+
throw new ContextErrorException($errstr, $errno, E_WARNING, $errfile, $errline, $errcontext);
215+
}, E_WARNING);
216+
217+
try {
218+
session_write_close();
219+
restore_error_handler();
220+
} catch (ContextErrorException $e) {
221+
// The default PHP error message is not very helpful, as it does not give any information on the current save handler.
222+
// Therefore, we catch this error and trigger a warning with a better error message
223+
$handler = $this->getSaveHandler();
224+
if ($handler instanceof SessionHandlerProxy) {
225+
$handler = $handler->getHandler();
226+
}
227+
228+
restore_error_handler();
229+
trigger_error(sprintf(
230+
'session_write_close(): Failed to write session data with %s handler',
231+
get_class($handler)
232+
), E_USER_WARNING);
233+
}
212234

213235
$this->closed = true;
214236
$this->started = false;

‎src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public function __construct(\SessionHandlerInterface $handler)
3535
$this->saveHandlerName = $this->wrapper ? ini_get('session.save_handler') : 'user';
3636
}
3737

38+
/**
39+
* @return \SessionHandlerInterface
40+
*/
41+
public function getHandler()
42+
{
43+
return $this->handler;
44+
}
45+
3846
// \SessionHandlerInterface
3947

4048
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.