2

It is frustrating to get error reports from users via email. I bet that most users wouldn't send a bug report to developer. Therefore, I wanna set up that if the user get PHP error codes, I would like to get the error reports automatically via email.

Is there a way to get user error reports in PHP or CodeIgniter?

Thank you.

1
  • What kind of errors we are talking about here? Commented Dec 30, 2012 at 14:13

3 Answers 3

1

You can do something like this:

<?php  
// Our custom error handler  
function email_error_handler($number, $message, $file, $line, $vars)  
{  
    $email = " 
        <p>An error ($number) occurred on line 
        <strong>$line</strong> and in the <strong>file: $file.</strong> 
        <p> $message </p>";  
    $email .= "<pre>" . print_r($vars, 1) . "</pre>";  
    $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
    // Email the error to someone...  
    error_log($email, 1, '[email protected]', $headers);  
    // Make sure that you decide how to respond to errors (on the user's side)  
    // Either echo an error message, or kill the entire project. Up to you...  
    // The code below ensures that we only "die" if the error was more than  
    // just a NOTICE.  
    if ( ($number !== E_NOTICE) && ($number < 2048) ) {  
        die("There was an error. Please try again later.");  
    }  
}  
// We should use our custom function to handle errors.  
set_error_handler('email_error_handler'); 
// Trigger an error... (var doesn't exist)  
echo $somevarthatdoesnotexist;  

Source

Sign up to request clarification or add additional context in comments.

Comments

0

PHP logs usually to the apache error log (if you're using apache), which is usually at /var/log/httpd/error_log.

CodeIgniter's error log, if enabled in config.php, is set to log by default to your application/log/date.php (Where date is today's date).

If you want, you can set a cron that emails you every hour of new changes in either (or both) files. Or you can create your own PHP error handler using set_error_handler function in PHP.

Edit: mbinette has pasted a good example that uses PHP's set_error_handler function. One caveat, this won't let you know about fatal 500 errors, the error log will.

Comments

0

You can have a look at the codeigniter's documentation about error reporting http://ellislab.com/codeigniter/user-guide/general/errors.html

There is a mention about error_reporting() function, probably you can hack it and do your email stuff.

1 Comment

Actually, error_reporting is only for setting the error reporting level. To do the email stuff, you'd have to use set_error_handler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.