What is the difference between input validation and input filtering in PHP?
Input validation is the process of ensuring that the input data meets certain criteria, such as being within a certain range or format. Input filtering, on the other hand, is the process of removing or modifying potentially harmful characters or data from the input. Both are important steps in securing web applications and preventing attacks such as SQL injection or cross-site scripting.
// Input validation example
$input = $_POST['username'];
if (preg_match('/^[a-zA-Z0-9]{5,}$/', $input)) {
// Valid input
} else {
// Invalid input
}
// Input filtering example
$input = $_POST['email'];
$filtered_input = filter_var($input, FILTER_SANITIZE_EMAIL);