Php session_start general error handling

I'm looking for a general way to handle session_start errors, not a way to deal with one specific error. There are many errors that can occur, such as a full session directory, that cause a fatal error. I want to catch these errors and handle them cleanly, without having to write custom handlers for every opportunity.

Something like this (but not this, as it does not work):

 try{ session_start(); }catch(Exception $e){ echo $e->getMessage(); } 

All help is appreciated, thanks in advance.

+4
source share
1 answer

Regular PHP session functions do not throw exceptions, but throw errors. Try writing an error handler function and installing an error handler before calling session_start .

 function session_error_handling_function($code, $msg, $file, $line) { // your handling code here } set_error_handler('session_error_handling_function'); session_start(); restore_error_handler(); 

However, this is just for capturing session errors. The best way would be to create a common error handler that throws exceptions from errors and parts of the spatial code that can cause errors using try ... catch blocks.

0
source

Source: https://habr.com/ru/post/1480971/


All Articles