tags:

views:

59

answers:

2

Hi While we run an web application some page may contain error and some page maynot contain error, I want to get a notification if the page contains some error ,If there is an error we can see the error in the page, but can we set any value to a variable if the page contains error.. such that we can get the notification that there is an error . I want to get the notification since i want to create an error log,If we can set the variable with some value then we can use some condition to create a logfile How can we do that?

+4  A: 

There is several ways to do it. One is to setup a custom error handler. PHP will trap most errors raised during script execution and pass it to your custom handler then. What you do inside the handler is up to you. You can write to a log and then redirect to somewhere else or whatever you want.

If you are talking about Exceptions, then wrap code that can break in try/catch blocks. If an error occurs, handle the exception the catch block. What you put in there is again up to you.

Go through the linked pages to learn how this works. Catching an error, setting a variable and writing to a log are three distinct things. Isolate and solve them one by one.

Gordon
@Gordon : i got the result but i didnt have idea how the page with error is recognised can you please give some examples if possible
udaya
@Gardon :this is not for project just to know what is happening
udaya
@udaya I do not understand what you mean by *how the page with error is recognised*
Gordon
@Gardon what i mean if error occurred then we are getting an error message in our log file .when there is no error then we dont have anything logged in the log file what is the functionality behind the process how the page is recognised as it contains error
udaya
@Gardon :have a look at my latest comment... If you dont get me make me an intimation i will explain problem in more detailed manner
udaya
@udaya Like I said in my answer. It's either a [PHP error](http://de3.php.net/manual/en/errorfunc.constants.php), an [Exception](http://de3.php.net/manual/en/reserved.exceptions.php) thrown somewhere (by PHP or you) or a Domain error, e.g something you defined as wrong enough to [trigger an error](http://de3.php.net/manual/en/function.trigger-error.php) or raise an Exception about it. Read through the links I gave please.
Gordon
@Gardon:yep sure i will go through those links
udaya
@Gardon :Does, My sql error that occurs due to some mistakes in query comes under Exception
udaya
@udaya MySQL is a database. It cannot throw PHP Exceptions or PHP Errors. Whether you get an error or exception from a PHP MySQL *extension* like PDO, MySQLi or the mysql_* functions depends on which you use. The PHP Manual usually notes when a function or method throws or triggers something. On a sidenote, could you please stop misspelling my name. Thanks!
Gordon
@Gordon: Sorryyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy i will not misspell again
udaya
+1  A: 

You could also consider using a try { } catch { } block and writing exceptions to error log in catch { } part. Like this:

try {

    $db = new MyDb('127.0.0.1', 'root', 'root');
    if (false === $db) {
        throw new Exception ('Could not connect to the database.');
    }

    $row = $db->getTable('table_name')->getRowByColumn('id', $_GET['id']);
    if (null === $row) {
        throw new Exception ('Row with id ' . $_GET['id'] . ' not found.')
    }

    // and so on

} catch (Exception $e) {
    $fp = fopen('logs/error.txt', 'w');
    fwrite($fp, date('l jS \of F Y h:i:s A') . ': ' . $e->getMessage() . "\n");
    fclose($fp);
}

You get the idea.

Instead of just a date of error you could also append a login of signed in user if the script is in authentication protected area so you know which user got that error.

Richard Knop