Ver Mensaje Individual
  #1 (permalink)  
Antiguo 12/02/2010, 01:08
Avatar de neodani
neodani
 
Fecha de Ingreso: marzo-2007
Mensajes: 1.811
Antigüedad: 17 años, 2 meses
Puntos: 20
Tratar los errores php de una pagina con una funcion de error propia

Buenas,

Estaba probando una función para crear tus propios errores PHP

http://www.stanford.edu/dept/its/com...andling_in_PHP

Pero cuando la pongo en un script forzando a que aparezca el error x ej. que no existe time_stamp2() no me crea el fichero de error que le estoy especificando y me escribe en el fichero de log por defecto:

[12-Feb-2010 08:01:50] PHP Fatal error: Call to undefined function time_stamp2() in C:\AppServ\www\00-laboratorio_test\secciones\errores_php.php on line 161

errores_php.php
Código PHP:
Ver original
  1. <?
  2. // Destinations
  3. define("ADMIN_EMAIL", "[email protected]");
  4. define("LOG_FILE", "mi_php_errors.log");
  5.  
  6. // Destination types
  7. define("DEST_EMAIL", "1");
  8. define("DEST_LOGFILE", "3");
  9.  
  10. /**
  11.   * my_error_handler($errno, $errstr, $errfile, $errline)
  12.   *
  13.   * Author(s): thanosb, ddonahue
  14.   * Date: May 11, 2008
  15.   *
  16.   * custom error handler
  17.   *
  18.   * Parameters:
  19.   *  $errno:   Error level
  20.   *  $errstr:  Error message
  21.   *  $errfile: File in which the error was raised
  22.   *  $errline: Line at which the error occurred
  23.   */
  24.  
  25. function my_error_handler($errno, $errstr, $errfile, $errline)
  26. {  
  27.   switch ($errno) {
  28.     case E_USER_ERROR:
  29.       // Send an e-mail to the administrator
  30.       error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_EMAIL, ADMIN_EMAIL);
  31.  
  32.       // Write the error to our log file
  33.       error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_LOGFILE, LOG_FILE);
  34.       break;
  35.  
  36.     case E_USER_WARNING:
  37.       // Write the error to our log file
  38.       error_log("Warning: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
  39.       break;
  40.  
  41.     case E_USER_NOTICE:
  42.       // Write the error to our log file
  43.       error_log("Notice: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
  44.       break;
  45.  
  46.     default:
  47.       // Write the error to our log file
  48.       error_log("Unknown error [#$errno]: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE);
  49.       break;
  50.   }
  51.  
  52.   // Don't execute PHP's internal error handler
  53.   return TRUE;
  54. }
  55.  
  56.  
  57. // Use set_error_handler() to tell PHP to use our method
  58. $old_error_handler = set_error_handler("my_error_handler");
  59. ?>
  60.  
  61. <?php
  62. function time_stamp($session_time)
  63. {
  64.  
  65. $time_difference = time() - $session_time ;
  66. $seconds = $time_difference ;
  67. $minutes = round($time_difference / 60 );
  68. $hours = round($time_difference / 3600 );
  69. $days = round($time_difference / 86400 );
  70. $weeks = round($time_difference / 604800 );
  71. $months = round($time_difference / 2419200 );
  72. $years = round($time_difference / 29030400 );
  73.  
  74. if($seconds <= 60)
  75. {
  76. echo"$seconds seconds ago";
  77. }
  78. else if($minutes <=60)
  79. {
  80.    if($minutes==1)
  81.    {
  82.      echo"one minute ago";
  83.     }
  84.    else
  85.    {
  86.    echo"$minutes minutes ago";
  87.    }
  88. }
  89. else if($hours <=24)
  90. {
  91.    if($hours==1)
  92.    {
  93.    echo"one hour ago";
  94.    }
  95.   else
  96.   {
  97.   echo"$hours hours ago";
  98.   }
  99. }
  100. else if($days <=7)
  101. {
  102.   if($days==1)
  103.    {
  104.    echo"one day ago";
  105.    }
  106.   else
  107.   {
  108.   echo"$days days ago";
  109.   }
  110.  
  111.  
  112.  
  113. }
  114. else if($weeks <=4)
  115. {
  116.   if($weeks==1)
  117.    {
  118.    echo"one week ago";
  119.    }
  120.   else
  121.   {
  122.   echo"$weeks weeks ago";
  123.   }
  124.  }
  125. else if($months <=12)
  126. {
  127.    if($months==1)
  128.    {
  129.    echo"one month ago";
  130.    }
  131.   else
  132.   {
  133.   echo"$months months ago";
  134.   }
  135.  
  136.    
  137. }
  138.  
  139. else
  140. {
  141. if($years==1)
  142.    {
  143.    echo"one year ago";
  144.    }
  145.   else
  146.   {
  147.   echo"$years years ago";
  148.   }
  149.  
  150.  
  151. }
  152.  
  153.  
  154.  
  155. }
  156. $session_time ="1264326122";
  157. //$session_time=time();
  158.  
  159. echo "<h1>";
  160. echo time_stamp2($session_time);
  161. echo "</h1>";
  162.  
  163. ?>

¿Sabéis si hay que cambiar alguna cosa más en el script?
Los efectos de definir las variables:

Código PHP:
Ver original
  1. define("ADMIN_EMAIL", "[email protected]");
  2. define("LOG_FILE", "mi_php_errors.log");
  3.  // Destination types
  4. define("DEST_EMAIL", "1");
  5. define("DEST_LOGFILE", "3");

¿Solo se aplicarían a esta página o afectarían a todo el site?

Aprovecho el post para preguntaros tb si creéis mejor que los logs se guarden en una base de datos o en un fichero de texto.
http://www.stanford.edu/dept/its/com..._logs_with_PHP

Muchas gracias de antemano!