1. <?php
  2. /**
  3. * Wraps URL parameter errors for monitoring purposes.
  4. *
  5. * This function intercepts errors related to URL parameters and logs them
  6. * for monitoring, providing compatibility with older PHP versions.
  7. *
  8. * @param array $env Environment variables (for logging)
  9. * @return array|null Modified array with error handling
  10. */
  11. function wrapUrlParameterErrors(array $env = []): ?array
  12. {
  13. // Check if the 'error_reporting' level is low enough to enable detailed error reporting
  14. if (error_reporting() < E_ALL) {
  15. error_reporting(E_ALL);
  16. }
  17. // Use a custom error handler to catch and log errors related to URL parameters
  18. set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$env): void {
  19. // Check if the error is related to URL parameters (simplified check, adjust as needed)
  20. if (strpos($errstr, 'Invalid parameter') !== false || strpos($errstr, 'Undefined index') !== false || strpos($errstr, 'ArgumentCountError') !== false) {
  21. // Log the error (replace with your actual logging mechanism)
  22. $logMessage = sprintf(
  23. 'URL Parameter Error: %s - File: %s, Line: %s',
  24. $errstr,
  25. $errfile,
  26. $errline
  27. );
  28. // Add environment variables to the log message
  29. $logMessage .= ' - Environment: ' . json_encode($env);
  30. error_log($logMessage, 3, '/path/to/your/error.log'); // Replace with your log file path. Use 3 for appending.
  31. // Re-throw the error to prevent it from being suppressed
  32. trigger_error($errstr, $errno, $errfile, $errline);
  33. } else {
  34. // Handle other errors (optional - you might want to log them too)
  35. trigger_error($errstr, $errno, $errfile, $errline);
  36. }
  37. });
  38. // Process URL parameters (example - modify as needed)
  39. $urlParams = $_GET;
  40. // Restore the original error handler (important!)
  41. restore_error_handler();
  42. return $urlParams;
  43. }
  44. //Example usage
  45. // $env = ['some_env_var' => 'some_value'];
  46. // $urlParams = wrapUrlParameterErrors($env);
  47. // print_r($urlParams);
  48. ?>

Add your comment