How to set custom error in php? - php

I am working on php online testing IDE.
I want to display error like http://phpfiddle.org/
Example
Code
Result
-----------------------------------------------------------
I want errors without the filepath.
I am trying with set_error_handler() but it is not working on parse error and fatal error.

You can use register_shutdown_function(), then within the called function, you may use debug_backtrace() for tracing, function, line, code & etc.

Related

Php change fatal error format

I would like to try to change the formatting of fatal error but I can only modify the values of message, file and line.
I would expect to be able to change the css and the html of the message.
Thanks in advance.
Fatal errors in PHP are catchable, so you can make your own exception handler or even use a third party package like https://github.com/filp/whoops
You can catch fatal errors in PHP. Refer to this answer here:
https://stackoverflow.com/a/4410769/9051466
After determining if its a fatal error, you can just create a custom page, redirect the user to that custom page, then display what error message you want the user to see.

How to include GET & POST data in PHP error log

Is there a way to have PHP log errors to a file or email me errors INCLUDING $_POST[] & $_GET[] and $_SERVER[] data?
Right now I get a log of PHP FATAL and WARNING errors and 404 NOT_FOUND errors but it's hard to debug some errors without knowing things like user input and the referrer.
Thanks
error_log(print_r($_POST, true));
error_log(print_r($_GET, true));
Put that into a custom error handler and it'll log both for you (the 'true' parameter makes print_r return text instead of outputting it).
You might need to boost the max line length in the error log with log_errors_max_len, as it defaults to 1024 chars and will truncate everything after that (it won't split >1024 char data across multiple lines).
If you are using PHP 5 create a custom exception class which will extend the native PHP Exception class and add whatever data collecting / logging methods you like. That way you could easily combine try {} catch() {} block with Exceptions and have your own way of controlling how you want to log all data.
I would recommend the error_log() function. I use that a lot to debug stuff in php.
http://php.net/manual/en/function.error-log.php
I worked on a telecom project that used CI to handle a Felx based frontend, we hijacked all possible error reporting and wrote a wrapper on syslog functions and used it in our application, the result was we could log whatever data we wanted and the log was visible through system log viewer :-)

How do I incorporate the line number of php source code in a custom error message?

I want to do my proper error generator when i'm programming (HTML + PHP).
How can i take the line, when i have an error, and put in a variable?
Example :
echo " Error # 03: variable undefined line #".$line." ";
Thanks.
the variables you'd be looking for are:
__LINE__
__FILE__
__FUNCTION__
__CLASS__
there is a predefined constant, __LINE__ that contains the line where it was actually called.
However, I guess that trigger_error() function perfectly fits "error generator" term, thus being exactly what you're looking for.
It will not only show you a line and a file and a timestamp, but also will follow general behavior of PHP error reporting settings, which is very important - you should never echo errors implicitly but rather put it into standard error stream
for the custom error handler there is also a debug_backtrace() function.
Assuming you are referring to PHP compile time errors and warnings, the line number is automatically displayed. Since these messages are generated at compile time (and as such can cause the script not to execute fully) I would recommend using the default messages rather than using a custom solution.
If PHP is not displaying the error messages, use the following code to display all of the PHP error messages and warnings on a page:
error_reporting(E_ALL);

PHP script stops without error message (Which error type?)

a PHP script stops without an error message, if I change the signature of a method of a class, which implements a intereface, e.g.:
interface A
{
public function somefunction();
}
class B implements A
{
public function somefunction(XY $xy);
{
...
}
}
This is an error of course, but there is no error message shown.
What is the name of this error type? (I already searched a lot, but with the wrong phrases obviously)
How can I log or output this error?
I'm using PHP 5.3.1 (with XAMPP for Windows 1.7.3)
(I used Zend Debugger with PHP < 5.3 earlier, where those erros were shown in the Eclipse console, but now I'm using XDebug.)
Thanks in advance for any hint!
put at the top of file and then try,
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
?>
If you still getting no output, please check your error_log.
RESOLVED
#ontrack Thanks, your hint directed me to the right direction:
I'm using an autoload function to load required classes (by using spl_autoload_register()). My implemention of my autoloader restrains all error messages... I did not know, that this causes such 'deeper' errors not to show up.
This was at least kind of stupid from my side, but I'm more happy, that I found the reason for this problem and I have learned something :-)
Many thanks to all your contributions! And sorry again, that I cannot edit my initial question anymore.
#outis Thanks, please read my comment
Put the following at the top of your script:
error_reporting(E_ALL);
It should report an error.
Something must be up with your configuration. When I try the sample code under PHP 5.3.2+XDebug 2.1.0, PHP complains:
Fatal error: Declaration of B::somefunction() must be compatible with that of A::somefunction()
The type of error is a substitution violation, since a B can't be substituted for an A in all situations.

Unknown problem with PHP's XML parsing functions

I'm using a PHP4 implementation of SimpleXML, which uses the built-in xml_* functions from PHP 4. I have an odd problem that I'm unable to diagnose due to no error reporting on the server and not being able to turn on error_reporting.
I've modified the Parse() function to include this:
[stuff here to initialise the parser]
echo '<textarea rows="8" cols="50">', htmlspecialchars($this->xml), '</textarea>';
$parsed = xml_parse($this->parser, $this->xml) or die('error with xml_parse function');
The textarea displays the XML fine, and the XML itself is perfectly valid. But the page stops right after that and doesn't appear to call the xml_parse function, or output the 'die' message.
Should also add that this works fine on other pages, it just appears to be a problem with this particular page for some reason.
What could be happening here? Are there other ways to debug this?
Yes, you can debug this by adding this to your script:
error_reporting(E_ALL);
Thus will turn on error reporting.
Also, using ‘or die‘ only works if the function call before it returns a falsey value. In the event of fatal errors, it doesn't help.
Try to set the error reporting on and see what error comes though, it is likely there is some fatal error coming up:
ini_set('display_errors', true);
error_reporting(E_ALL);
Try getting the XML specific parser error message:
echo xml_error_string(xml_error_code($parser));
IIRC, these are not output by default no matter what your error reporting is set to.
Reference:
xml_get_error_code
xml_get_error_string
It seems that the problem was due to the XML being too big for the parser. Since we can't turn on error_reporting there was no way to get proper debugging info.
I decided to use a separate script the generate the HTML for the page, to avoid issues with the page going down occasionally.

Categories