What are the potential pitfalls of not properly parsing URLs in PHP, especially when dealing with query parameters?

Not properly parsing URLs in PHP, especially when dealing with query parameters, can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To mitigate these risks, it is important to properly sanitize and validate any user input coming from URLs before using it in your application.

// Example of properly parsing and sanitizing a URL in PHP
$url = "https://www.example.com/page.php?name=John%20Doe&age=30";
$parsed_url = parse_url($url);
parse_str($parsed_url['query'], $query_params);

$safe_name = filter_var($query_params['name'], FILTER_SANITIZE_STRING);
$safe_age = filter_var($query_params['age'], FILTER_VALIDATE_INT);

// Now you can safely use $safe_name and $safe_age in your application