Solved

Log PHP errors in database

Posted on 2007-03-19
14
260 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:Cornelia Yoder
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
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
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:Cornelia Yoder
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

717 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