Example of function to send out email if error occurs – PHP Scripts

<?php 
 
   function reported() 
   { 
    
      $domain = ‘mydomain.com’; 
 
      // ————————————————————— 
 
      if( isset($_COOKIE&#91;‘error’]) ) 
      { 
        $_COOKIE&#91;‘error’] = unserialize( $_COOKIE&#91;‘error’] ); 
    
        if( in_array($_SERVER&#91;‘REQUEST_URI’], $_COOKIE&#91;‘error’]) ) 
        { 
          return TRUE; 
        } 
    } 
 
    $_COOKIE&#91;‘error’]&#91;] = $_SERVER&#91;‘REQUEST_URI’]; 
    $value = serialize( $_COOKIE&#91;‘error’] ); 
    setcookie( ‘error’, $value, time() + 24*60*60, ‘/’, ‘./’.$domain, 0 ); 
    unset( $domain, $value ); 
    return FALSE; 
   } 
 
   function error($error=’Undefined’) 
 
   { 
     if( !reported() ) 
     { 
      $to = ‘someone@example.com’; 
    
      $subject = « Error type $error has occured on $_SERVER&#91;‘REQUEST_URI’] »; 
    
      // set the MESSAGE: e-mail 
      $message =  » $error has occured on page:\n » 
            . »  //www.{$_SERVER&#91;‘SERVER_NAME’]}{$_SERVER&#91;‘REQUEST_URI’]}\n » 
            . »  Referring Page: « .( isset($_SERVER&#91;‘HTTP_REFERER’]) ? $_SERVER&#91;‘HTTP_REFERER’] : ‘Unknown’ ). » \n » 
            . »  Time: « .date(‘r’). »\n » 
            . »  From IP: {$_SERVER&#91;‘REMOTE_ADDR’]}\n\n » 
            . »Please Investigate the problem ASAP »; 
 
      $headers = « From: webserver@{$_SERVER&#91;‘SERVER_NAME’]}\n » 
             . »Reply-To: webserver@{$_SERVER&#91;‘SERVER_NAME’]}\n » 
             . »X-Mailer: PHP/ ».phpversion(); 
 
      // send the email 
      mail( $to, $subject, $message, $headers ); 
      } 
   } 
 
 
/* 
EXAMPLE for a 404 error page. 
error($error=’404 Error’); 
*/ 
?>