Solved

Log PHP errors in database

Posted on 2007-03-19
14
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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:
Mark Gilbert 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:Mark Gilbert
ID: 18759270
Hi Sypder, glad I was able to assist you.  Good luck with your project.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

730 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