How can extracted values from URLs be securely used in PHP scripts to prevent security vulnerabilities?

When extracting values from URLs in PHP scripts, it is important to sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One way to securely use extracted values is to use PHP's filter_var() function with the FILTER_SANITIZE_STRING filter to clean the input and ensure it contains only valid characters.

// Example of securely using extracted values from URLs in PHP
$url_param = $_GET['param']; // Assuming 'param' is the parameter extracted from the URL

// Sanitize the input using filter_var() with FILTER_SANITIZE_STRING
$sanitized_param = filter_var($url_param, FILTER_SANITIZE_STRING);

// Use the sanitized parameter in your PHP script
echo "Sanitized parameter: " . $sanitized_param;