<?php
/**
* Flags anomalies in URL parameters for staging environments.
* Logs anomalies to a file.
*
* @param array $params An array of URL parameters.
* @param string $stage The environment (e.g., 'staging').
* @param string $logFile The file to log anomalies to.
*/
function flagStagingUrlParams(array $params, string $stage = 'staging', string $logFile = 'url_param_anomalies.log'): void
{
// Define anomaly rules for staging environment. Adjust as needed.
$anomalyRules = [
'debug' => true, // Debug parameter should not be present in staging
'test' => true, // Test parameter should not be present in staging
'secret' => true, // Secret parameter should not be present in staging
'dev' => true, // Development related parameters should not be in staging
];
$anomalies = [];
foreach ($params as $key => $value) {
if (isset($anomalyRules[$key]) && $anomalyRules[$key]) {
$anomalies[] = [
'parameter' => $key,
'value' => $value,
'url' => $_SERVER['REQUEST_URI'] //Current URL
];
}
}
if (!empty($anomalies)) {
// Log anomalies
$logMessage = sprintf("Staging environment - URL parameter anomalies detected:\n%s\n", json_encode($anomalies, JSON_PRETTY_PRINT));
error_log($logMessage, 3, $logFile); // Log to file, level 3 (warning)
// Optionally, trigger an alert or action. Example:
// triggerAlert('Staging URL Parameter Anomaly');
}
}
// Example Usage (for testing)
/*
$testParams = [
'id' => 123,
'debug' => 'true',
'name' => 'Test User',
'test' => 'enabled',
'email' => 'test@example.com'
];
flagStagingUrlParams($testParams);
$testParams2 = [
'id' => 456,
'name' => 'Another User',
'email' => 'another@example.com'
];
flagStagingUrlParams($testParams2);
*/
?>
Add your comment