Solved

Log PHP errors in database

Posted on 2007-03-19
14
256 Views
Last Modified: 2013-12-13
I want to send my PHP errors to a MySQL database and then take the user to a generic error.html page. Is this possible?
0
Comment
Question by:sypder
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 27

Expert Comment

by:yodercm
ID: 18752640
What php errors?
0
 
LVL 3

Author Comment

by:sypder
ID: 18752680
Whatever PHP error occurs (due to a bug). I want to have them recorded in a DB so that I can go back and see what bugs might exist in my code.
0
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 18752721
bug normally means logical error and to catch logical errors are much more difficult than catching syntax error.
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 9

Expert Comment

by:richdiesal
ID: 18752749
What this really means: major syntax errors likely will not allow your script to run in the first place (an error code would occur), and thus users would never experience them in the first place - which means it's something you would have caught long before users even got access to them.

Logical errors, though, are bugs that don't produce a PHP syntax error but are instead situations where the output you expected is not the output that you received.  This kind of error won't be caught by PHP, as no violation of syntax has occured, and PHP assumes that whatever you did, you meant to do.

If you want to catch PHP warnings or notices, though, that's a different issue.  Although in such cases, I doubt you would want to send users to an error page, as such bugs don't necessarily make your script work improperly.
0
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 18752784
get latest error by
       http://www.php.net/manual/en/function.error-get-last.php
and insert into DB one by one and then display those to users.
0
 
LVL 3

Author Comment

by:sypder
ID: 18752802
I have found that there can be syntax errors as a result of very special cases, and I would just like these recorded. I know that PHP can record these to the log.

I was just wondering if I could get PHP to take someone to error.php if an error occurs and then use the error_get_last command to record it to a DB.
0
 
LVL 27

Expert Comment

by:yodercm
ID: 18752928
Unfortunately, I don't think you can trap syntax errors right in the php.
0
 
LVL 3

Author Comment

by:sypder
ID: 18752937
Thanks yodercm, I figured that might be the case. I would be handing rather than waiting for the client to report an error to me.
0
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 18752991
you can try something with try/catch, e.g.

<?
try
{
    echo 'aamir';
}
catch( Exception $e )
{
    die($e);
}
?>

if you print
  echo 'aamir'            // without semicolon at the end it 'll not produce error and print aamit

but if you have it in try/catch statement, it 'll give you error
0
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 18753053
also

The previous error message: $php_errormsg

$php_errormsg is a variable containing the text of the last error message generated by PHP. This variable will only be available within the scope in which the error occurred, and only if the track_errors configuration option is turned on (it defaults to off).
0
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 18753089
also

Since PHP 4.2.0, PHP returns an appropriate error code along with the file array. The error code can be found in the error segment of the file array that is created during the file upload by PHP. In other words, the error might be found in
   $_FILES['userfile']['error']
0
 
LVL 18

Accepted Solution

by:
ingwa earned 500 total points
ID: 18757788
Do your own custom error handling by using the following code:

<?php
set_error_handler('myerrors');
?>
What the code above does is tell PHP how you want to handle errors.  By the above we are telling php that if an error is encountered, it should run a function called myerrors.

You can then setup a function that can process that error and log it to your database.  Here's some pseudo code (Heavily edited so as not to infringe on copyright):

<?php
function myerrors($number, $string, $file, $line, $message){

echo "There was an error!";

error_log($error, 3, '/path/to/my/error/log'); // this allows you to log it to your error file.  Change this with your database code to handle it more efficiently
}
?>

Source of highly edited excerpts A Guide to Building Secure Web Applications

Hope this helps.
0
 
LVL 3

Author Comment

by:sypder
ID: 18758523
Thanks ingwa! That works amazingly well.
0
 
LVL 18

Expert Comment

by:ingwa
ID: 18759270
Hi Sypder, glad I was able to assist you.  Good luck with your project.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question