How to change a setting to true in a PHP file? - php

I have an Apache2 web server with PHP 5.5 installed.
My default PHP settings is display_error = 0 (I don't need globally displayed errors) but I need it on in specific PHP files.
I tried with:
error_reporting(E_ALL);
ini_set("display_errors", 1);
and it's not working.
Can someone tell me how can I make it show errors in specific PHP files?

i am try to force some error writing some no syntax logic and not showing error...
To show parse errors in PHP you have to put this on your php.ini
display_errors = on
My advice is to avoid displaying errors on production servers but log them all. So you can later inspect and fix bugs from yoursite-error.log file.
You should be concerned if your applications has warnings, errors etc. IMHO it is a bad idea to focus your attention only on few files instead of them all.
Although display_errors may be set at runtime (with ini_set()), it won't have any effect if the script has fatal errors. This is because the desired runtime action does not get executed.

Related

I've turned display_errors to On but I don't see any errors on the webpages

I'm trying to see what error PHP is producing. So I've changed the value of dispaly_errors to ON in the etc/php5/apache2/php.ini file.
The file doesn't display anything and I don't see any error on the webpages.
Am I missing any thing?
First, you have to make sure that this is your correct ini file. Usually the file you have used is the correct one. If not sure, you can create a simple PHP program to call the phpinfo() function and check this out.
Next, you have to restart Apache. Without a restart your settings don't take effect.
Another thing... This file can be a little misleading because there are so many comments in it. The actual line to change is way down. On my setup (LAMP/Ubuntu) the setting is on line 538.
Open php.ini file from your php folder, remove semicolon from all error reporting like
;error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT, ;display_errors=On etc, at last, restart your server, you will find all error messages.
Another way for showing error, you can write these codes in your script -
echo '<pre>';
error_reporting(E_ALL);
ini_set('display_errors', 1);
In addition to enabling display_errors, you may also need to set the error reporting level. if you are expecting errors with a script that is redirecting, be sure to turn off the redirection or you may never see them.

php error reporting for single page?

Is it possible (and if so, how) to turn on php error reporting to the screen for just one page? I currently have all error reporting to the screen switched off in php ini.
Or another way of asking - if I use error_reporting(E_ALL) on a single page, will it also display errors on other pages (what I don't want)?
Thanks!
Using error_reporting(E_ALL) will enable error reporting for only that page!
To display errors use, ini_set("display_errors", 1)
If you call error_reporting() within a PHP script, it will only affect that script's runtime (i.e. anything it executes after that call, including other scripts which have been included). It won't modify server configuration etc.
However, it's worth noting that if the script encounters an error before it can make that call, it won't have any effect (i.e. error reporting behaviour will be unchanged). That can be a particular problem if you have a syntax error or similar.
EDIT: It occurs to me that you may want to leave error reporting enabled in your configuration, but only enable display_errors as needed. That way you can still get errors written to a log file, but they'll only appear on-screen when you want.
Call error_reporting(0) at the beginning of a script to do this. It will block all runtime errors. However, it isn't recommended to do this, except possibly in production code.
Read more about error_reporting() here

show error instead of "Server Error"

I have recently changed hosts, on my old host if i had an error in my syntax the error would be displayed (showing me where the error was)
On my new host i do not see this, i just see
The website encountered an error while retrieving http://www.XXX.co.uk/delete_product.php?q=66550. It may be down for maintenance or configured incorrectly.
Is there any way i can show the error instead of this?
Turn on error reporting.
Include these lines are the top of your script:
ini_set('display_errors',1);
error_reporting(E_ALL);
If you have access to edit the php.ini file, you can edit it and include the following option:
error_reporting = E_ALL
These settings will help you troubleshoot code faster and makes it easy to identify errors. However, it is not appropriate for a production-level use. You should use the first method and then you can remove the lines once you've fixed the issues. On local development environments, it's okay to edit php.ini file and add the directive as mentioned above.
On production systems, do not use ini_set('display_errors', 1); as it can show information you might want to keep hidden. Use the server's logs instead. By default apache for example logs these errors in error_log.
And, anything that is open to the general internet public is considered "production" in my opinion. Development means it is a server sitting in your own local network.
Turning on error reporting would work, but perhaps it would be better to look into the server logs.

Fatal PHP Errors in IIS 7.5

I'd like to see any PHP errors that are occuring, ie the "Expected ; on line 5 of myfile.php" sort of thing. Unfortunately, I cannot seem to figure out how to see this information.
I've set E_ALL, display_errors ON, friendly error messages are turned off, IIS is set to pass-through on errors, what am I missing?
Syntax errors used to show up as stated above on any page; they no longer do. We moved the server to a localhost for development, and I guess didn't mimic exactly the server config. Now I'm stumped.
Tried on IE and Chrome, neither of which show the errors.
Errors are logged in PHP's log file, but I'd still like them to be displayed on the page; at least for now.
UPDATE:
Just tried adding ini_set('display_errors', 'on'); directly into the requested page, and it now works.. but why? Why does it need to be set locally? My PHP.ini file has this declared already.
To answer the first part of the question; to see the errors when using ajax: You can use the developer tools of your browser to see the exact response from the server.
In FireBug for Firefox for example, you go to the Net tab and there you see all ajax request popping up as they happen. Opening one of these requests will give you an overview with more tabs like Response and HTML.
Try using:
error_reporting (-1);
E_ALL isn't really "all" for php < 5.4.
Also, make sure 'display_errors' is set.
ini_set( 'display_errors', 1 );
Well, looks like this is half my own stupidity, half the cloudiness of automatic installations.
Turns out there were TWO php.ini files, and that IIS used the one located within the iis express directory on the main drive, instead of the regular PHP directory.
So to anybody else having this problem, I'm providing the full list of crap you have to wade through to get the errors as you would like:
1) Turn off the IIS default error pages
2) Disable 'friendly error messages'
3) Ensure you are using the CORRECT php.ini file, and change the parameters as needed. Specifically error_reporting and display_errors.
All of this is necessary before seeing all of the error messages you need right in the browser.

What to do when no errors and error-reporting enabled?

Im lost here, closed few files and clicked yes on save all and now page wont load and error_reporting(1); its in all documents... No erors given
If your error isn't a syntax error, you can manually debug by placing die('test'); in various areas of your code to see if that portion executes.
You also need to turn on the display of errors, so at the top of your file add:
error_reporting(E_ALL);
ini_set("display_errors", 1);
Turn on error_reporting and display_errors in your php.ini file. If you have a syntax errror in the script, most likely the script's being killed long before execution could ever reach in the in-script ini_set/error_reporting overrides.
You might want to check the server's error log, as fatal errors generally get sent there if error reporting/display is turned off.

Categories