PHP Form Sanitization
In this page:
$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
$email = " [email protected] ";
$sanitized = trim($email);
echo "[" . $sanitized . "]";
// Sanitizing cleans input; validating separately checks it's acceptable
?>
Login to try C/C++/Java/PHP code in the editor
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
$comment = "<b>Hello</b> World";
echo htmlspecialchars($comment);
// Many old filter_var sanitize filters are deprecated in PHP 8.1+
?>
Login to try C/C++/Java/PHP code in the editor
Numbers Sanitize करना
trim() stray leading/trailing whitespace हटाता है जो users अक्सर copy-pasting से गलती से डाल देते हैं, जो अन्यथा एक exact-match comparison (जैसे एक coupon code check) को unexpectedly fail करवा सकता है।
उदाहरण: Sanitizing Numbers
<?php
// Declare `$coupon`, set to " SAVE10 "
$coupon = " SAVE10 ";
// Print "[" . trim($coupon) . "]" to the output
echo "[" . trim($coupon) . "]";
?>
Login to try C/C++/Java/PHP code in the editor
Whitespace Strip करना
Input sanitize करना database safety के लिए parameterized queries की जगह नहीं लेता — sanitization risk कम करता है, लेकिन prepared statements ही actually SQL injection रोकते हैं, और दोनों साथ इस्तेमाल होने चाहिए, एक की जगह दूसरा नहीं।
उदाहरण: Stripping Whitespace
<?php
$username = trim(" alice ");
// Sanitizing reduces risk, but prepared statements prevent SQL injection
echo $username;
?>
Login to try C/C++/Java/PHP code in the editor
HTML Tags हटाना
सबसे safe general rule यह है कि storage के लिए input को आते समय sanitize करें, और HTML में जाते समय output को अलग से (htmlspecialchars() के साथ) encode करें, क्योंकि सही escaping data कहाँ इस्तेमाल हो रहा है इस पर depend करता है।
उदाहरण: Removing HTML Tags
<?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;
?>
Login to try C/C++/Java/PHP code in the editor
- sanitize करना जब आपको validate करना चाहिए, ताकि bad input reject होने के बजाय बदल दिया जाए।
- save करते समय
htmlspecialcharsइस्तेमाल करना, display करते समय के बजाय। - input पर
trimभूल जाना, ताकि extra spaces store हो जाएँ।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: