How to show code errors while running - php

previously i had php5.1 installed and i recently upgraded to php5.3.
In previous versions all syntax errors and other errors were display in page whenever error occurs and it was easy for me to debug.
But now whenever there is error in page it just stops processing and shows blank.
i tried adding following too:
error_reporting(E_ALL & E_STRICT);
ini_set('display_errors', 1);
at top of page but didn't work. Any suggestions?
Edit more info:
when i do phpinfo(), it shows my configuration file: c:\php\php.ini.
Further on opening and editing php.ini file i found:
error_reporting = E_ALL
display_errors = On
Edit More Info:
Corrected error_reporting to E_ALL | E_STRICT.
Found it almost blanks out whenever the error is being generated from code inside function or class.

try this.
ini_set('display_errors','On');

whenever there is error in page
What level of error? Is it fatal or not?
If fatal - then your in-code settings (error_reporting() and ini_set) are not used at all: php.ini ones are used instead.
IMO, you've provided insufficient info: at least php.ini values you have for error_reporting and error_log directives needed.

Related

PHP 7. I'm not able to display or log errors below. Is this possible? [duplicate]

I'm running ubuntu 10.04 + nginx + php-fpm 5.4
If I set display_errors = On in php.ini all errors are printed. If Instead I set that to off and then use ini_set('display_errors, '1'); directly in the script they will show as well but not the parse errors, just a blank page. I tried to play with error_reporting too and E_STRICT but I couldn't find a way!
If you disable display_errors in php.ini, and later enable it in your PHP script using ini_set(), it will only be enabled after the line containing that ini_set() call has been executed.
Parse errors occur before the PHP script even starts -- when the PHP file is parsed (hence the "parse error" name).
Which means they occur before your ini_set() has even a chance of being executed -- which means that, in your case, display_errors is not enabled when the parse error occurs ; and, as a consequence, you don't get anything displayed.
here I am years after this was answered, but I found a way around this.
For the script I'm writing, I created a second script which includes the ini_set() directives, followed by an include for the script I really am working on.
To with, here is test_run.php
<?php
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
include('./my_script.php');
?>
Aside from turning on display_errors, you can also watch error logs. Typically, running Ubuntu + apache, your error log is going to be in /var/log/apache2/error_log. To watch in real time what's happening, you run something like tail -f /var/log/apache2/error_log.
This can sometimes be more straightforward than fussing with php settings.
**You must enable error displaying in the php.ini file **
I have added ( un-commented ) the following lines, and the parse errors are being displayed now.
error_reporting
Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
Development Value: E_ALL
Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
Try error_reporting(E_ALL);. Or the docs

I have disabled PHP Error reporting in php.ini but it continues to show in browser

So I have disabled PHP Errors inside my php.ini file (/etc/php5/apache2/php.ini) but they still continue to show in browser. I am running PHP version 5.5.9-1ubuntu4.13 and to do this I used the following:
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE & ~E_WARNING
display_errors = Off
display_startup_errors = Off
Examples of errors still showing include:
Warning: Creating default object from empty value ...
and
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in ...
Any help/advice as to why these errors may still be showing would be greatly appreciated. Thanks!
So I finally figured this out, with the help of Ukavi in the comments.
The errors started to display after I had transferred the site from one server to another, and it appears that the first server ran an older version of PHP which is why they did not show up before.
Anyway, the fix. I added this to wp-config.php:
error_reporting(0);
ini_set('display_errors', 'off');
I also did a find in files for ini_set and found a few instances where they had been deliberately activated. As this site is not a solo project I wasn't aware of this!
Thank you for all the advice guys.

Not getting PHP errors?

For some reason on this particular script, which is a copy of a script I use in a lot of other places, I am not getting any PHP errors. It simply shows a blank page, and It took me a long long time to hunt down a missing semi-colon this morning. Why arn't errors showing up?
my PHP.INI for this sub-domain:
display_errors = On
short_open_tag = On
memory_limit = 32M
date.timezone = Europe/Paris
The code at the top of the page:
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
The Sub Domain is set to run PHP as an Apache Module Which is the same as every other domain I use.
So I am not sure why I am not getting errors displayed. Can anyone tell me?
EDIT:
This is solved, because the errors I was producing were on the page where I had got the lines:
error_reporting(E_ALL);
ini_set('display_errors', '1');
written. When I put the error on to a seperate page and included it, I could see the error fine.
I guess that's why they use bootstrapping!
You should set error_reporting to E_ALL in the php.ini as well: when a parse error occurs (such as a missing semicolon), your error_reporting(E_ALL) won't be used.
You could try to change this lines to get more information from php:
; The display of errors which occur during PHP's startup sequence are handled
; separately from display_errors. PHP's default behavior is to suppress those
; errors from clients. Turning the display of startup errors on can be useful in
; debugging configuration problems. But, it's strongly recommended that you
; leave this setting off on production servers.
display_startup_errors = On
; When PHP displays or logs an error, it has the capability of formatting the
; error message as HTML for easier reading. This directive controls whether
; the error message is formatted as HTML or not.
; Note: This directive is hardcoded to Off for the CLI SAPI
html_errors = On
error_reporting(E_ALL);
ini_set('display_errors',1);
ini_set('display_startup_errors',1);

Turn off display errors using file "php.ini"

I am trying to turn off all errors on my website. I have followed different tutorials on how to do this, but I keep getting read and open error messages. Is there something I am missing?
I have tried the following in my php.ini file:
;Error display
display_startup_errors = Off
display_errors = Off
html_errors = Off
docref_root = 0
docref_ext = 0
For some reason when I do a fileopen() call for a file which does not exist, I still get the error displayed. This is not safe for a live website, for obvious reasons.
I always use something like this in a configuration file:
// Toggle this to change the setting
define('DEBUG', true);
// You want all errors to be triggered
error_reporting(E_ALL);
if(DEBUG == true)
{
// You're developing, so you want all errors to be shown
display_errors(true);
// Logging is usually overkill during development
log_errors(false);
}
else
{
// You don't want to display errors on a production environment
display_errors(false);
// You definitely want to log any occurring
log_errors(true);
}
This allows easy toggling between debug settings. You can improve this further by checking on which server the code is running (development, test, acceptance, and production) and change your settings accordingly.
Note that no errors will be logged if error_reporting is set to 0, as cleverly remarked by Korri.
You should consider not displaying your error messages instead!
Set ini_set('display_errors', 'Off'); in your PHP code (or directly into your ini file if possible), and leave error_reporting on E_ALL or whatever kind of messages you would like to find in your logs.
This way you can handle errors later, while your users still don't see them.
Full example:
define('DEBUG', true);
error_reporting(E_ALL);
if (DEBUG)
{
ini_set('display_errors', 'On');
}
else
{
ini_set('display_errors', 'Off');
}
Or simply (same effect):
define('DEBUG', true);
error_reporting(E_ALL);
ini_set('display_errors', DEBUG ? 'On' : 'Off');
In php.ini, comment out:
error_reporting = E_ALL & ~E_NOTICE
error_reporting = E_ALL & ~E_NOTICE | E_STRICT
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ER… _ERROR
error_reporting = E_ALL & ~E_NOTICE
By placing a ; ahead of it (i.e., like ;error_reporting = E_ALL & ~E_NOTICE)
For disabling in a single file, place error_reporting(0); after opening a php tag.
In file php.ini you should try this for all errors:
error_reporting = off
Let me quickly summarize this for reference:
error_reporting() adapts the currently active setting for the default error handler.
Editing the error reporting ini options also changes the defaults.
Here it's imperative to edit the correct php.ini version - it's typically /etc/php5/fpm/php.ini on modern servers, /etc/php5/mod_php/php.ini alternatively; while the CLI version has a distinct one.
Alternatively you can use depending on SAPI:
mod_php: .htaccess with php_flag options
FastCGI: commonly a local php.ini
And with PHP above 5.3 also a .user.ini
Restarting the webserver as usual.
If your code is unwieldy and somehow resets these options elsewhere at runtime, then an alternative and quick way is to define a custom error handler that just slurps all notices/warnings/errors up:
set_error_handler(function(){});
Again, this is not advisable, just an alternative.
In file php.ini you should try this for all errors:
display_errors = On
Location file is:
Ubuntu 16.04:/etc/php/7.0/apache2
CentOS 7:/etc/php.ini
You can also use PHP's error_reporting();
// Disable it all for current call
error_reporting(0);
If you want to ignore errors from one function only, you can prepend a # symbol.
#any_function(); // Errors are ignored
Turn if off:
You can use error_reporting(); or put an # in front of your fileopen().
I usually use PHP's built in error handlers that can handle every possible error outside of syntax and still render a nice 'Down for maintenance' page otherwise:
Format PHP error on production server
Open your php.ini file (If you are using Linux - sudo vim /etc/php5/apache2/php.ini)
Add this lines into that file
error_reporting = E_ALL & ~E_WARNING
(If you need to disabled any other errors -> error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE & ~E_WARNING)
display_errors = On
And finally you need to restart your APACHE server.
It is not enough in case of PHP fpm. It was one more configuration file which can enable display_error. You should find www.conf. In my case it is in directory /etc/php/7.1/fpm/pool.d/
You should find php_flag[display_errors] = on and disable it, php_flag[display_errors] = off. This should solve the issue.
It's been quite some time and iam sure OP's answer is cleared. If any new user still looking for answer and scrolled this far, then here it is.
Check your updated information in php.ini file
Using Windows explorer:
C/xampp/php/php.ini
Using XAMPP Control Panel
Click the Config button for 'Apache' (Stop | Admin | Config | Logs)
Select PHP (php.ini)
Can i create my own phpinfo()? Yes you can
Create a phpinfo.php in your root directly or anywhere you want
Place this <?php phpinfo(); ?>
Save the file.
Open the file and you should see all the details.
How to set display_errors to Off in my own file without using php.ini
You can do this using ini_set() function. Read more about ini_set() here (https://www.php.net/manual/en/function.ini-set.php)
Go to your header.php or index.php
add this code ini_set('display_errors', FALSE);
To check the output without accessing php.ini file
$displayErrors = (ini_get('display_errors') == 1) ? 'On' : 'Off';
$PHPCore = array(
'Display error' => $displayErrors
// add other details for more output
);
foreach ($PHPCore as $key => $value) {
echo "$key: $value";
}

How to have PHP display errors? (I've added ini_set and error_reporting, but just gives 500 on errors)

So, I don't really have any errors in my current web page, but I want to be able to see an error when they pop up, instead of the HTTP 500 error page. I googled around a bit and thought adding these two lines would fix everything.
ini_set('display_errors', 'On');
error_reporting(E_ALL);
NOTE: I don't have access to the php.ini file, as I'm using my school account's server.
So I introduced a bug (no semicolon after $buggy) like so at the top of my page:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$buggy
$x = 4 + 2;
...
However, I just get a Server error:
"The website encountered an error while retrieving http://mywebpage.com/. It may be down for maintenance or configured incorrectly."
Any ideas?
EDIT:
I've reconfigured my code:
<?php
include_once 'database/errorSettings.php';
?>
<?php
$buggy // whoops
$x = 4 + 2;
...
errorSettings.php is the following:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
?>
But it still doesn't work... wrong way to reconfigure?
What you have is a parse error. Those are thrown before any code is executed. A PHP file needs to be parsed in its entirety before any code in it can be executed. If there's a parse error in the file where you're setting your error levels, they won't have taken effect by the time the error is thrown.
Either break your files up into smaller parts, like setting the error levels in one file and then includeing another file which contains the actual code (and errors), or set the error levels outside PHP using php.ini or .htaccess directives.
You need to set the error_reporting value in a .htaccess file. Since there is a parse error, it never runs the error_reporting() function in your PHP code.
Try this in a .htaccess file (assuming you can use one):
php_flag display_errors 1
php_value error_reporting 30719
I think 30719 corresponds to E_ALL but I may be wrong.
Edit Update: http://php.net/manual/en/errorfunc.constants.php
int error_reporting ([ int $level ] )
---
32767 E_ALL (integer)
All errors and warnings, as supported, except of level E_STRICT prior to PHP 5.4.0. 32767 in PHP 5.4.x, 30719 in PHP 5.3.x, 6143 in PHP 5.2.x, 2047 previously
Adding to what deceze said above. This is a parse error, so in order to debug a parse error, create a new file in the root named debugSyntax.php. Put this in it:
<?php
/////// SYNTAX ERROR CHECK ////////////
error_reporting(E_ALL);
ini_set('display_errors','On');
//replace "pageToTest.php" with the file path that you want to test.
include('pageToTest.php');
?>
Run the debugSyntax.php page and it will display parse errors from the page that you chose to test.
Just write a following code on top of PHP file:
ini_set('display_errors','on');
Syntax errors is not checked easily in external servers, just runtime errors.
What I do? Just like you, I use
ini_set('display_errors', 'On');
error_reporting(E_ALL);
However, before run I check syntax errors in a PHP file using an online PHP syntax checker.
The best, IMHO is PHP Code Checker
I copy all the source code, paste inside the main box and click the Analyze button.
It is not the most practical method, but the 2 procedures are complementary and it solves the problem completely
I have had this problem when using PHP5.4 and Plesk 11.5
Somehow, the error reporting and display error settings in the Plesk domain configuration page were completely overriding any local settings in .htaccess or the PHP scripts. I have not found a way to prevent this happening, so use the Plesk settings to turn error reporting on and off.
You may have settings in your php.ini that prevents the local site from overriding these settings, perhaps enforced by the control panel used on your server.
To people using Codeigniter (i'm on C3):
The index.php file overwrite php.ini configuration, so on index.php file, line 68:
case 'development':
error_reporting(-1);
ini_set('display_errors', 1);
break;
You can change this option to set what you need. Here's the complete list:
1 E_ERROR
2 E_WARNING
4 E_PARSE
8 E_NOTICE
16 E_CORE_ERROR
32 E_CORE_WARNING
64 E_COMPILE_ERROR
128 E_COMPILE_WARNING
256 E_USER_ERROR
512 E_USER_WARNING
1024 E_USER_NOTICE
6143 E_ALL
2048 E_STRICT
4096 E_RECOVERABLE_ERROR
Hope it helps.

Categories