We are using post_max_size and upload_max_filesize PHP variables to prevent users from uploading big files into our web app.
The thing is that the error message that PHP throws is ugly for the users and does not say much (especially for spanish users). So the users report this as a bug or think it is not working.
How can I change this page and show our own page (something more user friendly and in Spanish)?
Thanks a lot!
I think you're looking for set_error_handler().
So something along the lines of (from the manual):
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}
switch ($errno) {
case UPLOAD_ERR_INI_SIZE:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
$old_error_handler = set_error_handler("myErrorHandler");
catch the error and output a custom message.
try {
// your code
} catch (Exception $e) {
if($e->getCode() == some code){
$message = 'some message';
}else{
$message = 'some other message';
}
}
echo '<div>'.$message.'</div>'
Related
I'm having difficulty debugging my PHP Application.
When I include my php file, no exception is thrown. I tried using set_error_handler, which does fire, but error_get_last() returns null.
set_error_handler(function() {
echo 'Here is your error!';
var_dump(error_get_last());
echo 'Sike!';
});
try {
include('viewer.php');
} catch (Exception $e) {
echo 'I really ought to tell you something went wrong here. But I won\'t';
}
restore_error_handler();
exit();
Output:
Here is your error!NULL Sike!
Is there some third way I can find out what actually went wrong?
from php docs try this
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting, so let it fall
// through to the standard PHP error handler
return false;
}
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
set_error_handler("myErrorHandler");
I'm trying to get error logging working so that errors don't show on my live website to people other than me (identified by my IP address), but it's not working because apparently ini_set() is disabled for my server due to "security reasons." Here's the code I was trying to use:
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', '/path/to/php-error.log');
I don't want to hard code the option to disable showing errors, because I do a check in my PHP pages to see if it's my IP address visiting the website and if so still show the errors.
Is there any way to log errors, dynamically changing whether they are displayed on the page or not, without using ini_set()?
You should take a look at PHP's in-built custom error handling functions, which you could build your verification into. This replaces PHP's error handler with a custom function.
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
$ip = $_SERVER['REMOTE_ADDR'];
if ( $ip != '127.0.0.1' ) return true; // Leave if not your IP
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
set_error_handler("myErrorHandler");
More info from the manual at: http://www.php.net/manual/en/function.set-error-handler.php
I have an interface calling some php function by ajax.
There is an uncatched error that I have only in production. Due I make estensive use of getJson data exchange , I needed to silent errors and warnings with a custom php error handler.
The problem is that anyway when code generates an error in production I wish to print it out to the user, to understand where the problem is. My code is :
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_ERROR:
case E_USER_ERROR:
$error= "<b>My ERROR</b> [$errno] $errstr<br />\n";
$error.= " Fatal error on line $errline in file $errfile";
$error.= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
$error.= "Aborting...<br />\n";
if(isAjaxCalling()){
$_SESSION['Errors']['Errors'][]=$error;
}
ini_set('display_errors',1);
ini_set('error_reporting','E_ALL');
echo $error;
exit(1);
break;
case E_WARNING:
case E_USER_WARNING:
....
}
You need to let know the ajax handler on the client side that the returned value is in fact an error message, best to do it with status header:
header("HTTP/1.0 500 Internal server error");
Then either globaly, or individualy per ajax request, set function that will handle the error. In jQuery, how to do it globaly: http://api.jquery.com/ajaxError/ or individualy set 'error' option of ajax request.
Seems it's now printed with this mod:
case E_ERROR:
case E_USER_ERROR:
$error= "<b>My ERROR</b> [$errno] $errstr<br />\n";
$error.= " Fatal error on line $errline in file $errfile";
$error.= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
$error.= "Aborting...<br />\n";
if(isAjaxCalling()){
$_SESSION['Errors']['Errors'][]=$error;
}
exit( $error);
break;
I am developing an website but when am escaping then inserting records into database, I am using try catch.
I want to create a global error array which I can add to any exception message if the try fails and catching an exception.
This array would later be used on my php page to show the error that occurred to the user.
How would I go about creating this array?
Thanks,
You can create a generic error handler like this one:
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
// set to the user defined error handler
set_error_handler("myErrorHandler");
I'm writing a script, where a lot of things could go wrong. I'm making if/else statements for the obvious things, that could heppen, but is there a way to catch something, that could possible heppen, but I don't know what it is yet?
For example something causes an error of some kind, in the middle of the script. I want to inform the user, that something has gone wrong, but without dozens of php warning scripts.
I would need something like
-- start listening && stop error reporting --
the script
-- end listening --
if(something went wrong)
$alert = 'Oops, something went wrong.';
else
$confirm = 'Everything is fine.'
Thanks.
Why not try...catch?
$has_errors = false;
try {
// code here
} catch (exception $e) {
// handle exception, or save it for later
$has_errors = true;
}
if ($has_errors!==false)
print 'This did not work';
Edit:
Here is a sample for set_error_handler, which will take care of any error that happens outside the context of a try...catch block. This will also handle notices, if PHP is configured to show notices.
based on code from: http://php.net/manual/en/function.set-error-handler.php
set_error_handler('genericErrorHandler');
function genericErrorHandler($errno, $errstr, $errfile, $errline) {
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
$v = 10 / 0 ;
die('here');
Read up on Exceptions:
try {
// a bunch of stuff
// more stuff
// some more stuff
} catch (Exception $e) {
// something went wrong
}
throw new Exception('Division by zero.');
try {
echo inverse(5) . "\n";
echo inverse(0) . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
http://php.net/manual/en/language.exceptions.php
You should definitely use the try-catch syntax to catch any exception thrown by your script.
Additionally you can extend exceptions and implement new ones that fulfill your needs.This way, you can throw your own exceptions when you find any other kind of unexpected error (error for your script's logic).
A very short example explaining the use of extending exceptions :
//your own exception class
class limitExceededException extends Exception { ... }
try{
// your script here
if($limit > 10)
throw new limitExceededException();
}catch(limitExceededException $e){//catching only your limit exceeded exception
echo "limit exceeded! cause : ".$e->getMessage();
}catch(Exception $e){//catching all other exceptions
echo "unidentified exception : ".$e->getMessage();
}
Besides using try/catch, I think it's important to consider if you should catch an unexpected error. If it's unexpected then your code has no idea how to handle it and allowing the application to continue may produce bad data or other incorrect results. It may be better to just let it crash to an error page. I just recently had a problem where someone had added generic exception handlers to everything, and it hid the original location of the exception making the bug difficult to find.