1. <?php
  2. /**
  3. * Exports URL parameters for routine automation with basic input validation.
  4. *
  5. * @return array|false An array of validated URL parameters, or false on error.
  6. */
  7. function exportUrlParameters() {
  8. // Get all URL parameters.
  9. $params = get_url_parameters();
  10. // Validate and sanitize parameters.
  11. $validatedParams = [];
  12. foreach ($params as $key => $value) {
  13. $validatedParams[$key] = validateParameter($key, $value);
  14. }
  15. return $validatedParams;
  16. }
  17. /**
  18. * Retrieves URL parameters.
  19. *
  20. * @return array An associative array of URL parameters.
  21. */
  22. function get_url_parameters() {
  23. $params = [];
  24. if (isset($_GET)) {
  25. $params = $_GET;
  26. }
  27. return $params;
  28. }
  29. /**
  30. * Validates and sanitizes a single parameter.
  31. *
  32. * @param string $key The parameter key.
  33. * @param string $value The parameter value.
  34. * @return mixed The validated and sanitized parameter value, or false on error.
  35. */
  36. function validateParameter($key, $value) {
  37. // Basic validation: Check if the parameter exists and is not empty.
  38. if (empty($value)) {
  39. error_log("Invalid parameter: " . $key . " is empty."); // Log the error
  40. return false;
  41. }
  42. //Basic type validation: Check if the value is an integer.
  43. if (is_numeric($value)) {
  44. $value = (int)$value; //cast to int
  45. }
  46. //Sanitize the input. Important for security.
  47. $value = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
  48. return $value;
  49. }
  50. // Example usage (uncomment to test - remove before deployment)
  51. //$results = exportUrlParameters();
  52. //if ($results !== false) {
  53. // print_r($results);
  54. //} else {
  55. // echo "Error exporting URL parameters.";
  56. //}
  57. ?>

Add your comment