Input validation is the process of checking if the data provided by the user meets the expected format, type, or rules before using it in the program.
Click to reveal answer
beginner
What does input sanitization mean?
Input sanitization means cleaning or modifying user input to remove or escape harmful characters to prevent security issues like SQL injection or cross-site scripting.
Click to reveal answer
beginner
Which PHP function can be used to check if an email is valid?
The function filter_var() with the FILTER_VALIDATE_EMAIL flag checks if an email address is valid.
Click to reveal answer
intermediate
How does htmlspecialchars() help in input sanitization?
htmlspecialchars() converts special characters to HTML entities, preventing malicious code from running in the browser.
Click to reveal answer
beginner
Why should you never trust user input directly?
User input can be incorrect or harmful. Validating and sanitizing input protects your program from errors and security attacks.
Click to reveal answer
Which PHP function is used to validate an integer input?
Afilter_var() with FILTER_VALIDATE_INT
Bhtmlspecialchars()
Ctrim()
Dstrtolower()
✗ Incorrect
filter_var() with FILTER_VALIDATE_INT checks if the input is a valid integer.
What does the PHP function trim() do?
AEscapes quotes in a string
BConverts special characters to HTML entities
CChecks if input is a valid email
DRemoves whitespace from the beginning and end of a string
✗ Incorrect
trim() removes spaces or other whitespace characters from the start and end of a string.
Why is input sanitization important?
ATo change the input type
BTo prevent security risks like SQL injection and XSS
CTo make the input longer
DTo speed up the program
✗ Incorrect
Sanitization cleans input to stop harmful code from causing security problems.
Which filter validates an email address in PHP?
AFILTER_VALIDATE_INT
BFILTER_SANITIZE_EMAIL
CFILTER_VALIDATE_EMAIL
DFILTER_SANITIZE_STRING
✗ Incorrect
FILTER_VALIDATE_EMAIL checks if the input is a valid email format.
What is the main difference between validation and sanitization?
B. FILTER_SANITIZE_INT should be FILTER_VALIDATE_INT
C. Using $_POST without quotes
D. filter_var() cannot validate integers
Solution
Step 1: Understand difference between sanitize and validate
FILTER_SANITIZE_INT cleans input by removing non-numeric characters but does not validate if input is a valid integer.
Step 2: Correct filter for validation
To check if input is a valid integer, use FILTER_VALIDATE_INT instead of FILTER_SANITIZE_INT.
Final Answer:
FILTER_SANITIZE_INT should be FILTER_VALIDATE_INT -> Option B
Quick Check:
Validation needs FILTER_VALIDATE_INT, not sanitize [OK]
Hint: Use FILTER_VALIDATE_INT to check integers, not sanitize [OK]
Common Mistakes:
Confusing sanitize with validate filters
Ignoring filter_var() return values
Assuming sanitize validates input
5. You want to accept a username that only contains letters, numbers, and underscores. Which PHP code correctly validates this input using filter_var() and a custom pattern?
B. $username = $_POST['username'];
if (filter_var($username, FILTER_SANITIZE_STRING)) {
echo "Valid username";
} else {
echo "Invalid username";
}
C. $username = $_POST['username'];
if (filter_var($username, FILTER_VALIDATE_EMAIL)) {
echo "Valid username";
} else {
echo "Invalid username";
}
D. $username = $_POST['username'];
if (filter_var($username, FILTER_VALIDATE_INT)) {
echo "Valid username";
} else {
echo "Invalid username";
}
Solution
Step 1: Understand the requirement
The username must only have letters, numbers, and underscores, so a regular expression is needed to check this pattern.
Step 2: Use FILTER_VALIDATE_REGEXP with correct pattern
FILTER_VALIDATE_REGEXP allows validating input against a regex pattern. The pattern '/^[a-zA-Z0-9_]+$/' matches only allowed characters.
Step 3: Eliminate distractors
The incorrect options use filters for integer validation, string sanitization, or email validation, none of which enforce the specific pattern of letters, numbers, and underscores.
Final Answer:
Code using FILTER_VALIDATE_REGEXP with pattern '/^[a-zA-Z0-9_]+$/' -> Option A
Quick Check:
Validate username with FILTER_VALIDATE_REGEXP + regex [OK]
Hint: Use FILTER_VALIDATE_REGEXP with regex for custom pattern validation [OK]
Common Mistakes:
Using sanitize instead of validate
Using email or int filters for username
Not using regex for pattern matching
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.