How to find out if there is an error on the page, and if there is something to make a redirect?
- what does it mean "is there an error on the page"? Ie on the client side? Or at the moment when the page on the server side is formed? - UserName
|
2 answers
Use set_error_handler , set_exception_handler , register_shutdown_function in conjunction with error_reporting(E_ALL); .
function toHome(){ header('Location: /'); die(); } error_reporting(E_ALL); set_error_handler('toHome'); set_exception_handler('toHome'); register_shutdown_function(function(){ if(@error_get_last() !== null) toHome(); exit; }); // Fatal error some(); Attention!
Using redirects in this way is potentially fraught with errors in the style of ERR_TOO_MANY_REDIRECTS .
This can not be done in any case.
I captured it, sent it to the log, displayed a beautiful sign with an error for the user.
|
try { // код } catch (Exception $e) { header('Location: /'); } First, catch errors in the try block, and then, instead of throwing an exception, make a redirect (catch block)
- Generally any mistake. - asd
- For example, the page can have different errors, syntax error or a variable that does not exist. So, if an error is found on the page, then redirect to the main header ('location: /'); - asd
- @asd corrected the answer. - smellyshovel
- how to catch a mistake? - asd
- @asd don't understand you. The error itself is “caught” by the try block. If there is any error in the code that is executed inside the try block, the catch block will be called, which, in turn, will redirect. - smellyshovel
|