|
Like creating a file that stores the errors that have taken place during the execution of a program, to add an errors log to our page.
An errors log, will allow us to control when there is
produced an error to correct it and to prevent from repeating itself in
future.
To create a log, we will open the file in way "for" (it registers to
end) and we will write the error indicating the date, to simplify
we can include work everything in a function:
<? php
function error ($numero, $texto) {
$ddf = fopen ('error.log', 'to');
fwrite ($ddf, "[".date ("r")."] Error $numero: $texto\r\n");
fclose ($ddf);
}
?>
As soon as it was declared the function, tansolo we will have to call it of
following way when an error takes place so that he keeps in
error.log:
<? php
//If the cookie meeting does not exist
if (! isset ($_COOKIE ['meeting'])) {
//We keep an error
error ('001', 'the meeting cookie does not exist');
}
?>
This way, whenever a user enters this page without
cookie meeting, a new line is stored in the file indicating:
[date] Error 001: The meeting cookie does not exist
We are going to see now as we can improve this so that in addition to being able to record the errors that we define in our place, it stores to us the errors produced during the execution of the script php.
We will obtain this indicating the interpreter Zend who should call to
function error () whenever the code PHP contains an error with
function set_error_handler:
<? php
set_error_handler ('error');
?>
Then, we have left the finished code of the following way:
<? php
function error ($numero, $texto) {
$ddf = fopen ('error.log', 'to');
fwrite ($ddf, "[".date ("r")."] Error $numero:$texto\r\n");
fclose ($ddf);
}
set_error_handler ('error');
?>
And this way we give for our script finished to generate one
log of personal errors and of PHP.
Eloi of St Martin
http://www.programacionweb.net/articulos/articulo/?num=264 |