What if a tiny unchecked input could break your whole website or steal data?
Input validation vs sanitization in PHP - When to Use Which
Imagine you run a website where users type their names and emails. Without checking or cleaning this input, bad data or harmful code can sneak in.
Manually checking every input is slow and easy to forget. Mistakes let wrong or dangerous data cause errors or security holes.
Input validation checks if data looks right before using it. Sanitization cleans data to remove harmful parts. Together, they keep your site safe and working well.
$name = $_POST['name']; $email = $_POST['email']; // no checks or cleaning
$name = filter_var($_POST['name'], FILTER_SANITIZE_SPECIAL_CHARS); $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
It lets you trust user input so your app stays safe and runs smoothly.
When signing up for a newsletter, validation ensures the email looks real, and sanitization removes any strange code someone might try to sneak in.
Validation checks if input is correct.
Sanitization cleans input to remove bad parts.
Both protect your app from errors and attacks.
Practice
input validation in PHP?Solution
Step 1: Understand input validation meaning
Input validation means checking if the data entered by the user follows the expected format or rules, like checking if an email looks like an email.Step 2: Compare with other options
Removing unwanted characters is sanitization, storing data is unrelated, and encrypting is a different security step.Final Answer:
To check if the input data meets expected rules or format -> Option CQuick Check:
Validation = Check input format [OK]
- Confusing validation with sanitization
- Thinking validation changes the input
- Assuming validation stores or encrypts data
sanitizing an email input?Solution
Step 1: Identify sanitization function for email
FILTER_SANITIZE_EMAIL removes illegal characters from an email string, cleaning it.Step 2: Differentiate from validation and other functions
FILTER_VALIDATE_EMAIL checks if email is valid (validation). htmlspecialchars escapes HTML, trim removes spaces.Final Answer:
filter_var($email, FILTER_SANITIZE_EMAIL) -> Option DQuick Check:
Sanitize email = FILTER_SANITIZE_EMAIL [OK]
- Using FILTER_VALIDATE_EMAIL for sanitization
- Confusing htmlspecialchars with sanitization
- Ignoring trimming spaces as sanitization
$input = "user@example.com"; $sanitized = filter_var($input, FILTER_SANITIZE_EMAIL); echo $sanitized;
Solution
Step 1: Understand FILTER_SANITIZE_EMAIL effect
This filter removes illegal characters but keeps some like apostrophes and semicolons, so the alert part remains partially.Step 2: Analyze input and output
The <script> tags are removed, but alert('Hi'); remains before the email, so output is alert('Hi');user@example.com.Final Answer:
alert('Hi');user@example.com -> Option AQuick Check:
Sanitize email removes tags but keeps some chars [OK]
- Expecting full removal of script content
- Confusing sanitization with escaping HTML
- Assuming syntax error from special chars
$age = "25";
if (filter_var($age, FILTER_SANITIZE_NUMBER_INT)) {
echo "Valid age";
} else {
echo "Invalid age";
}Solution
Step 1: Check filter used for validation
FILTER_SANITIZE_NUMBER_INT cleans input but does not validate if it's a proper integer.Step 2: Correct filter for validation
FILTER_VALIDATE_INT should be used to check if input is a valid integer.Final Answer:
Using FILTER_SANITIZE_NUMBER_INT instead of FILTER_VALIDATE_INT -> Option AQuick Check:
Validation needs FILTER_VALIDATE_INT, not sanitize [OK]
- Confusing sanitize and validate filters
- Ignoring filter_var second argument importance
- Assuming sanitize also validates
Solution
Step 1: Understand validation and sanitization order
First sanitize the input to remove unwanted characters using FILTER_SANITIZE_STRING.Step 2: Validate length and alphanumeric
Then validate the length with strlen and check if the string is alphanumeric using ctype_alnum.Final Answer:
Sanitize with FILTER_SANITIZE_STRING, then validate length and ctype_alnum -> Option BQuick Check:
Sanitize first, then validate pattern and length [OK]
- Validating before sanitizing
- Skipping sanitization and trusting validation only
- Ignoring length check in validation
Ready to prove it?
Pass the test and this pattern moves unknown
