ChenXiu Posted September 2, 2021 Share Posted September 2, 2021 If my page has a PHP error, I want to immediately exit with the error displayed in my browser. Because I'm on a shared server (no access to php.ini file, and NO error logs!) I have to have this on top of my page: ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); However, I also need PHP to exit with the error message displayed. (The reason is some of the scripts reload to another page and I only see a milisecond flash of error message.) Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/313650-exit-on-php-error/ Share on other sites More sharing options...
gw1500se Posted September 2, 2021 Share Posted September 2, 2021 There are 2 ways. Use either try...catch or the less desirable 'die'. Quote Link to comment https://forums.phpfreaks.com/topic/313650-exit-on-php-error/#findComment-1589588 Share on other sites More sharing options...
Psycho Posted September 2, 2021 Share Posted September 2, 2021 Adding a try/catch or die to every single place that an error could potentially occur would probably be an exercise in futility. You say that your shared server has no error logs. You could try adding code to your page(s) to generate an error log. Put this code somewhere so that it will be executed on any page load: ini_set("log_errors", 1); ini_set("error_log", "php-errors.log"); You could then have a separate bowser window open to the location of the error log and refresh it upon any page load to see what errors may have occurred. Quote Link to comment https://forums.phpfreaks.com/topic/313650-exit-on-php-error/#findComment-1589590 Share on other sites More sharing options...
ginerjm Posted September 2, 2021 Share Posted September 2, 2021 With the ini settings you are making the errors will display automatically. Simply do the exit when you check for errors. I don't think you need to set the 'startup' setting since you are not doing any startup. Quote Link to comment https://forums.phpfreaks.com/topic/313650-exit-on-php-error/#findComment-1589593 Share on other sites More sharing options...
Barand Posted September 2, 2021 Share Posted September 2, 2021 You can't set display startup errors in the code. If you get a startup error, the code to tell them to display can't be executed. Quote Link to comment https://forums.phpfreaks.com/topic/313650-exit-on-php-error/#findComment-1589595 Share on other sites More sharing options...
ChenXiu Posted September 2, 2021 Author Share Posted September 2, 2021 2 hours ago, Psycho said: ini_set("error_log", "php-errors.log"); Thank you, this will help! The main problem I have is I have a javascript window.open -- the window opens and it flashes the error for a nanosecond and then reloads with correct html data. So I need to figure out what that error message was that only flashed for a nanosecond. Regarding my error reporting code (e.g. "ini_set('display_errors', '1');"), I don't know how (or where) to put the "exit;" in there. Quote Link to comment https://forums.phpfreaks.com/topic/313650-exit-on-php-error/#findComment-1589596 Share on other sites More sharing options...
ginerjm Posted September 2, 2021 Share Posted September 2, 2021 You add error checking to things that matter. If you are trying to open a file, check if you did. Running a query? Check for the results. Echo out an error message of your own and then exit. Any errors from php that you have asked to see will now show up on their own. You don't put the exit with the ini settings. Quote Link to comment https://forums.phpfreaks.com/topic/313650-exit-on-php-error/#findComment-1589598 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.