I have a CodeIgniter PHP app running on a Heroku Cedar instance.
We are running a PHP app and we need to log errors, but NOT print them to the screen. No matter what I do, the errors are printing to the screen, which is not safe for production.
Here is the PHP code which works on my local environment and everywhere else (besides Heroku):
error_reporting(E_ALL); #we care about all errors
ini_set('display_errors',0); #but DONT print to screen
I have seen this document which suggested I try adding a custom CodeIgniter logging class, which did not work.
I also added a phpinfo() to the app to check if somehow my settings were being overridden downstream, but it shows that display_errors is set to "Off".
So why am I STILL seeing errors printed on the screen?
It turns out that CodeIgniter 2.x has some completely asinine error handling in which they use a custom error handler to ignore the developers configurations. Apparently this is fixed in v3.
I was able to fix it by just commenting out the custom error handling, which for me was located in in CodeIgniter.php, line 72...
//set_error_handler('_exception_handler');
Or the best solution of all: don't use CodeIgniter in the first place.
Related
I have a form that allows upload three files at the same time but just one is required. That works fine, my only problem is the following: if I upload three files I haven't any problem but if I upload one or two files (leaving two or one files empties) I obtain the following notice:
Notice: No file uploaded in Unknown on line 0
As much as empty files. The files are uploaded properly without any other problem, but I want remove that notice... or unless hide it, although I prefer remove it. I tried to hide it using
error_reporting(0);
and
ini_set('display_errors',0);
but neither of two worked...
It is the first time that I have problem, if someone could lead me I'd be very grateful due to that I am stuck with it.
If you are having the same problem as me, check with phpinfo() if you are using a debug version of PHP. If you see that Debug Build has a value of yes, your problem will be fixed if you install a live version of PHP instead of a debug version
The Error itself is caused by running a Debug version of PHP 7, see the bug report. As noted by HPierce because it was a Debug build it overrides the usual PHP settings for error_reporting. However as the Original question is actually about how to hide certain [expected] error messages (Notices), my answer is to this detail specifically.
Kevin, the attempted ways to hide errors you've listed in your question would normally work on non-debug PHP builds. However, it is unwise to ignore the errors, rather than solving them at source. It's also (more) unwise to hide all errors simply due to having expected errors appearing.
As it's only a Notice, you can work around it by setting your error_reporting() value as below:
//report all errors except notices.
error_reporting(E_ALL & ~E_NOTICE);
I would suggest this is far wiser than turning off error reporting entirely which is not recommended. If you want to stop errors being output to browser (as referenced by Tina) you can use display_errors.
Perhaps you may also need to set
ini_set('error_reporting', 0);
depending on your php ini configuration?
Also make sure you set it before carrying out any of the code.
I am in the process of migrating codeigniter from centos to ubuntu and after moving the files, adjusting the config files, etc, I am getting the white screen of death. Debugging it, the code seems to die in CodeIgniter.php at
$CI = new $class();
where
$class = welcome
I have confirmed the MySQL is installed and running, both in PHP and on the command line. Additionally, I can't find codeigniter complaining about any errors in either my apache error logs or the codeigniter ones at application/logs. This is despite setting the logging threshold to 4 and setting display errors to true.
the only solution for this issue to find the php error logs,
check it on /var/logs/apache2/errors.log or if you use ssl /var/logs/apache2/ssl_errors.log
this is the default paths for php error log
please check your php error logs for getting the error message.
There's 3 solutions to know where's the problem.
1- Codeigniter offer logs for configure it
go to application/config.php
Change $config['log_threshold'] = 2; //for debug messages
Each request you can find the log messages in
application/logs/..
or
system/logs/..
Don't forget to switch it off after fix your issue by change it to 0 or 1 for display error messages only.
2- Or you can Enable development mode but i prefer the first solution.
if you want to switch to development mode edit index.php in the main project files and change ENVIRONMENT definition to 'development'
this option show errors on the site front.
3- Or you can check the PHP log errors in
/var/log/apache2/error.log
or
/var/log/apache2/ssl_error.log
if you using https
CodeIgniter 2.x still uses the classic mysql. We all know it's bad practice to still use it, but my job still requires me to use CodeIgniter.
I always have my Console.app (OSX) open watching my Apache/MySQL/PHP/CodeIgniter-native error logs.
Now I mostly have all notices/errors/etc. fixed always instantly when I see them and constantly monitor my development with Webgrind on my MAMP.
Back to the start; I constantly have one big annoying thing each page-load PHP always gives the error about mysql_pconnect is going to get deprecated in the future.
In the CodeIgniter driver the command is suppressed by # to not print the warnings to the screen, but it still ends up in my logs.
Is there any viable way to except one such error specifically in either PHP code or the PHP settings?
Locally I could recompile the whole PHP core and just remove the warning, but I would like to have the logs on our production installations rid of those warnings too :P.
Thanks in advance!
Traditionally, you can use set error verbosity using error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED) (i.e., report everything—except notices and deprecation warnings) as mentioned in "disabling deprecated errors".
Your issue may be related to CodeIgniter taking ownership of all errors.
system/core/CodeIgniter.php calls the function set_error_handler. You can find and modify the function _exception_handler it invokes on error in system/core/Common.php. It doesn't appear to be a configurable, so you may simply want to edit the line that begins with $is_error.
I have a php script that is really simple, but requires some of the wordpress includes. I have used the code from their website for most of it but it is failing whenever I try to call the require_once parts of the scripts. here is the relevant code:
$fn = dirname(__FILE__) . '/wp-admin/includes/media.php';
if(!file_exists($fn))
{
echo 'No File';
}
if(!is_readable($fn))
{
echo 'File is unreadable';
}
require_once $fn;
Interstingly enough, The only echo that I get when the require_once is uncommented is the full path to the document. The file is both existing and readable. However, when I uncomment the require_once code it comes back with a 500 error.
On a slightly related point. What is the easiest way of debugging php. I haven't found anything that is VS easy yet (or even as easy to debug as Django!!)
PHP has various settings in php.ini for how logging works. You can also set them at runtime. It might be logging to a file somewhere rather than displaying the error. Consider trying:
error_reporting(E_ALL)
To debug your application. Don't leave it on when you're done working with it.
You should be developing and debugging the code locally on your workstation, not on a shared hosting server. On your local workstation you have access to the php.ini, Apache error log, and the PHP error log. You can also set project-specific settings for debugging using .htaccess files. For example, you can configure separate PHP error log files for each different project you're working on.
As #Steve Howard pointed out, there are many settings you can alter in the php.ini in order to get more detailed error reporting. In fact, most LAMP packages install a development copy of the php.ini, which is basically a template of a php.ini file that's optimized for detailed error reporting. Detailed error reporting is something you want on your local environment for debugging. It is NOT something want on your production environment where customers and potential hackers will end up seeing more information than they should!
If you don't know how to set up PHP on your workstation, take a look at XAMPP, MAMP, Macports, etc. There are many options. You don't need to be super technical to get a stack set up on your computer. Once you have a good dev environment setup on your computer you can tail the PHP error log and the Apache log.
One thing I like to do is to use the PHP error_log() function. This allows you to output custom messages to the PHP error log. For example, if I'm debugging code and I need to know what the value of $foo is, I can do something like this:
<?php
error_log("Foo is: " . print_r($foo, true));
This is much better than using var_dump() or echo to print debugging data to the screen because it won't interrupt the normal execution of the program or the display of its view layer.
I would like to log PHP errors on a CakePHP site that has debug = 0. However, even if I turn on the error log, like this:
error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED
log_errors = On
it doesn't log errors.
The problem is that even for a parse error that should cause the CakePHP environment to not load completely (I think), it still blocks the error from being logged. If I set debug to 3, it logs to the file without issue.
I am using CakePHP 1.2. I know this is apparently made easier in 1.3, but I'm not ready to upgrade.
Another way to keep track of and log errors would be to use the Referee plugin as it provides a way to arbitrarily log and catch all (including fatal) errors that occur during exection.
define('LOG_ERROR', 2); in core.php
PHP should log errors to its own logfile, regardless of what CakePhp is doing.
Look in /etc/php.ini file (or wherever yours lives) and search for error_log. This will show you where the PHP log resides on your system.
There is a bug in CakePHP 1.2-1.3 where PHP errors/warnings are suppressed in view code when debugging is disabled.
In the file cake/libs/view/view.php on line #664 it reads
#include ($___viewFn);
But the # directive suppresses errors for the entire view handler. Instead it should be:
include ($___viewFn);
Which allows PHP errors/warnings to be generated in view code and subsequently get logged. Once I changed this and had the right logging settings in core.php I was finally able to get complete logs in production.
Sometime the reason could be very different. For example the framework you are using may have its own internal caching module which keeps the value in buffer while you keep on trying. Check whether duplicate copies are getting generated or not. Typically those files would be named as filename.ext.r123 and so on.