Skip to content
PHP Logo

How can PHP developers ensure that their code is secure when handling user input from URLs?

PHP developers can ensure that their code is secure when handling user input from URLs by validating and sanitizing the input before using it in their application. This can help prevent common security vulnerabilities such as SQL injection and cross-site scripting attacks. One way to achieve this is by using PHP's filter_var() function with the FILTER_SANITIZE_STRING filter to sanitize the input.

php
$input = $_GET['user_input'] ?? ''; // Get the user input from the URL
$filtered_input = filter_var($input, FILTER_SANITIZE_STRING); // Sanitize the input

// Use the sanitized input in your application
echo $filtered_input;