What potential pitfalls can arise from not properly handling form input in PHP scripts?

Improper handling of form input in PHP scripts can lead to security vulnerabilities such as SQL injection, cross-site scripting (XSS), and other types of attacks. To mitigate these risks, it is essential to sanitize and validate user input before using it in database queries or displaying it on the website.

// Example of sanitizing and validating form input in PHP

// Retrieve form input
$username = $_POST['username'];
$password = $_POST['password'];

// Sanitize input
$username = filter_var($username, FILTER_SANITIZE_STRING);
$password = filter_var($password, FILTER_SANITIZE_STRING);

// Validate input
if(empty($username) || empty($password)) {
    // Handle error, e.g. display an error message to the user
} else {
    // Proceed with using the sanitized input
}