<?php
/**
* Collects metrics on user input.
*
* This script gathers data like input length, type, and potential errors
* for internal tooling. It avoids external libraries for simplicity.
*/
// Function to record a metric
function recordMetric(string $metricName, mixed $value) {
global $metrics; // Access the global metrics array
$metrics[$metricName][] = $value;
}
// Array to store metrics
$metrics = [];
// Function to collect input metrics
function collectInputMetrics(string $inputName, string $userInput): void {
global $metrics;
// Data validation (example)
if (empty($userInput)) {
recordMetric("input_empty", true); // Record if input is empty
return;
}
// Input length
$inputLength = strlen($userInput);
recordMetric("input_length_" . $inputName, $inputLength);
// Input type (basic check) - could be improved with regular expressions
if (ctype_digit($userInput)) {
recordMetric("input_type_" . $inputName, "numeric");
} elseif (ctype_alpha($userInput)) {
recordMetric("input_type_" . $inputName, "alphabetic");
} else {
recordMetric("input_type_" . $inputName, "alphanumeric");
}
// Potential error detection (example)
if (strpos($userInput, "<script") !== false) {
recordMetric("input_contains_script_" . $inputName, true);
}
// Optionally, record the raw input (be mindful of sensitive data)
// recordMetric("raw_input_" . $inputName, $userInput);
}
// Example usage (Simulate user input – replace with your actual input source)
$input1 = $_POST['username'] ?? ''; // Example: Retrieve username from POST data
$input2 = $_POST['email'] ?? ''; // Example: Retrieve email from POST data
$input3 = $_POST['age'] ?? ''; // Example: Retrieve age from POST data
collectInputMetrics("username", $input1);
collectInputMetrics("email", $input2);
collectInputMetrics("age", $input3);
// Output the collected metrics (for debugging/tooling)
echo "<pre>";
print_r($metrics);
echo "</pre>";
?>
Add your comment