← Back to PHP Course | Chapter 10: Forms & Validation | Lesson 3 of 8

PHP Form Sanitization

Sanitization cooking से पहले vegetables धोने जैसा है: यह visitors द्वारा भेजी गई चीज़ को साफ करता है ताकि आपके page या files में कुछ harmful न पहुँचे। PHP dangerous characters को हटाता है या harmless में बदल देता है।
Syntax
php
$clean = trim($input);
$clean = htmlspecialchars($input);
$clean = filter_var($input, FILTER_SANITIZE_EMAIL);
$clean = filter_var($input, FILTER_SANITIZE_NUMBER_INT);

Emails Sanitize करना

Sanitization input को एक safe, expected form में साफ या transform करता है — unwanted characters strip करना या special characters encode करना — जबकि validation सिर्फ यह check करता है कि input acceptable है या नहीं; दोनों साथ काम करते हैं, interchangeably नहीं।

उदाहरण: Sanitizing Emails

php
<?php
$email = "  [email protected]  ";
$sanitized = trim($email);
echo "[" . $sanitized . "]";
// Sanitizing cleans input; validating separately checks it's acceptable
?>

Strings Sanitize करना

filter_var($value, FILTER_SANITIZE_STRING) और related filters user input से potentially dangerous characters strip या encode करते हैं, हालाँकि PHP के कई sanitize filters PHP 8.1+ में output time पर explicit encoding के favor में deprecated हैं।

उदाहरण: Sanitizing Strings

php
<?php
$comment = "<b>Hello</b> World";
echo htmlspecialchars($comment);
// Many old filter_var sanitize filters are deprecated in PHP 8.1+
?>

Numbers Sanitize करना

trim() stray leading/trailing whitespace हटाता है जो users अक्सर copy-pasting से गलती से डाल देते हैं, जो अन्यथा एक exact-match comparison (जैसे एक coupon code check) को unexpectedly fail करवा सकता है।

उदाहरण: Sanitizing Numbers

php
<?php
// Declare `$coupon`, set to "  SAVE10  "
$coupon = "  SAVE10  ";
// Print "[" . trim($coupon) . "]" to the output
echo "[" . trim($coupon) . "]";
?>

Whitespace Strip करना

Input sanitize करना database safety के लिए parameterized queries की जगह नहीं लेता — sanitization risk कम करता है, लेकिन prepared statements ही actually SQL injection रोकते हैं, और दोनों साथ इस्तेमाल होने चाहिए, एक की जगह दूसरा नहीं।

उदाहरण: Stripping Whitespace

php
<?php
$username = trim(" alice ");
// Sanitizing reduces risk, but prepared statements prevent SQL injection
echo $username;
?>

HTML Tags हटाना

सबसे safe general rule यह है कि storage के लिए input को आते समय sanitize करें, और HTML में जाते समय output को अलग से (htmlspecialchars() के साथ) encode करें, क्योंकि सही escaping data कहाँ इस्तेमाल हो रहा है इस पर depend करता है।

उदाहरण: Removing HTML Tags

php
<?php
// Declare `$input`, set to "<script>alert('x')</script>Hello"
$input = "<script>alert('x')</script>Hello";
// Declare `$forStorage`, set to `strip_tags($input)`
$forStorage = strip_tags($input);
// Declare `$forDisplay`, set to `htmlspecialchars($forStorage)`
$forDisplay = htmlspecialchars($forStorage);
// Print `$forDisplay` to the output
echo $forDisplay;
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. sanitize करना जब आपको validate करना चाहिए, ताकि bad input reject होने के बजाय बदल दिया जाए।
  2. save करते समय htmlspecialchars इस्तेमाल करना, display करते समय के बजाय।
  3. input पर trim भूल जाना, ताकि extra spaces store हो जाएँ।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.