Skip to content
PHP Logo

How can PHP developers differentiate between a valid URL and a potentially malicious input containing harmful characters?

To differentiate between a valid URL and potentially malicious input containing harmful characters, PHP developers can use the filter_var() function with the FILTER_VALIDATE_URL filter. This function will validate the input as a URL and return either true for a valid URL or false for potentially malicious input.

php
$url = "https://www.example.com";

if (filter_var($url, FILTER_VALIDATE_URL)) {
    echo "Valid URL";
} else {
    echo "Potentially malicious input";
}