1. Why Server-Side Validation is Mandatory
While HTML5 attributes like required and type="email" provide immediate UX feedback, client-side validation can be effortlessly bypassed using DevTools, cURL, or Postman. The server must validate and sanitize all incoming payloads.
2. Sanitization vs Validation
- Sanitization: Cleanses input by stripping unauthorized characters, whitespace, or scripts.
- Validation: Checks whether input conforms to expected criteria (e.g., length, numeric bounds, valid email pattern).
3. Using PHP filter_var()
";
} else {
echo "Invalid email format.
";
}
// Validating Integer with Range
$age = 22;
$intOptions = ["options" => ["min_range" => 18, "max_range" => 65]];
if (filter_var($age, FILTER_VALIDATE_INT, $intOptions)) {
echo "Age is within valid adult range.
";
}
// Validating URL
$website = "https://onlinecbt.in";
if (filter_var($website, FILTER_VALIDATE_URL)) {
echo "Valid web URL.
";
}
4. Structured Form Validation Pattern