1. <?php
  2. /**
  3. * Flags anomalies in URL parameters for staging environments.
  4. * Logs anomalies to a file.
  5. *
  6. * @param array $params An array of URL parameters.
  7. * @param string $stage The environment (e.g., 'staging').
  8. * @param string $logFile The file to log anomalies to.
  9. */
  10. function flagStagingUrlParams(array $params, string $stage = 'staging', string $logFile = 'url_param_anomalies.log'): void
  11. {
  12. // Define anomaly rules for staging environment. Adjust as needed.
  13. $anomalyRules = [
  14. 'debug' => true, // Debug parameter should not be present in staging
  15. 'test' => true, // Test parameter should not be present in staging
  16. 'secret' => true, // Secret parameter should not be present in staging
  17. 'dev' => true, // Development related parameters should not be in staging
  18. ];
  19. $anomalies = [];
  20. foreach ($params as $key => $value) {
  21. if (isset($anomalyRules[$key]) && $anomalyRules[$key]) {
  22. $anomalies[] = [
  23. 'parameter' => $key,
  24. 'value' => $value,
  25. 'url' => $_SERVER['REQUEST_URI'] //Current URL
  26. ];
  27. }
  28. }
  29. if (!empty($anomalies)) {
  30. // Log anomalies
  31. $logMessage = sprintf("Staging environment - URL parameter anomalies detected:\n%s\n", json_encode($anomalies, JSON_PRETTY_PRINT));
  32. error_log($logMessage, 3, $logFile); // Log to file, level 3 (warning)
  33. // Optionally, trigger an alert or action. Example:
  34. // triggerAlert('Staging URL Parameter Anomaly');
  35. }
  36. }
  37. // Example Usage (for testing)
  38. /*
  39. $testParams = [
  40. 'id' => 123,
  41. 'debug' => 'true',
  42. 'name' => 'Test User',
  43. 'test' => 'enabled',
  44. 'email' => 'test@example.com'
  45. ];
  46. flagStagingUrlParams($testParams);
  47. $testParams2 = [
  48. 'id' => 456,
  49. 'name' => 'Another User',
  50. 'email' => 'another@example.com'
  51. ];
  52. flagStagingUrlParams($testParams2);
  53. */
  54. ?>

Add your comment