1. <?php
  2. /**
  3. * Collects metrics on user input.
  4. *
  5. * This script gathers data like input length, type, and potential errors
  6. * for internal tooling. It avoids external libraries for simplicity.
  7. */
  8. // Function to record a metric
  9. function recordMetric(string $metricName, mixed $value) {
  10. global $metrics; // Access the global metrics array
  11. $metrics[$metricName][] = $value;
  12. }
  13. // Array to store metrics
  14. $metrics = [];
  15. // Function to collect input metrics
  16. function collectInputMetrics(string $inputName, string $userInput): void {
  17. global $metrics;
  18. // Data validation (example)
  19. if (empty($userInput)) {
  20. recordMetric("input_empty", true); // Record if input is empty
  21. return;
  22. }
  23. // Input length
  24. $inputLength = strlen($userInput);
  25. recordMetric("input_length_" . $inputName, $inputLength);
  26. // Input type (basic check) - could be improved with regular expressions
  27. if (ctype_digit($userInput)) {
  28. recordMetric("input_type_" . $inputName, "numeric");
  29. } elseif (ctype_alpha($userInput)) {
  30. recordMetric("input_type_" . $inputName, "alphabetic");
  31. } else {
  32. recordMetric("input_type_" . $inputName, "alphanumeric");
  33. }
  34. // Potential error detection (example)
  35. if (strpos($userInput, "<script") !== false) {
  36. recordMetric("input_contains_script_" . $inputName, true);
  37. }
  38. // Optionally, record the raw input (be mindful of sensitive data)
  39. // recordMetric("raw_input_" . $inputName, $userInput);
  40. }
  41. // Example usage (Simulate user input – replace with your actual input source)
  42. $input1 = $_POST['username'] ?? ''; // Example: Retrieve username from POST data
  43. $input2 = $_POST['email'] ?? ''; // Example: Retrieve email from POST data
  44. $input3 = $_POST['age'] ?? ''; // Example: Retrieve age from POST data
  45. collectInputMetrics("username", $input1);
  46. collectInputMetrics("email", $input2);
  47. collectInputMetrics("age", $input3);
  48. // Output the collected metrics (for debugging/tooling)
  49. echo "<pre>";
  50. print_r($metrics);
  51. echo "</pre>";
  52. ?>

Add your comment