PHP filter_var() Function In Hindi | filter_var() In PHP In Hindi
Bubble Sort in PHP With Example | How Bubble Sort Works in PHP
Insertion Sort in PHP With Example | How Does Insertion Sort Work in PHP
PHP Null Coalescing Operator In Hindi , Null Coalescing Operator kya hai ?
What is Gateway in Hindi ? Gateway kya hai ?
Laravel MySQL Best Pratice : Laravel Tips And Tricks
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Validate user input in PHP
किसी भी तरह के Modern web application में किसी भी type के data को store या process करने से पहले data को validate किया जाता है , ताकि applications को secure और reliable बनाया जा सके।
अगर आप अपने web application में कोई framework like Laravel , Codeigniter या CakePHP use कर रहे हैं तब तो user data validate करने के लिए ये frameworks ही default rules provide करते हैं। जिनकी help से आप input data को easy और effectively validate कर सकते हैं।
लेकिन अगर आप web application Core PHP में implement कर रहें हैं तो input data को manually validate करना पड़ेगा।
●●●
हालाँकि PHP में भी by default कुछ functionalities provide की हैं जिनकी help से हम external data को validate कर सकें। और, इस blog में हम यही सीखेंगे कि किस तरह से Core PHP में input data को effectively validate कर सकते हैं।
backend में input data को validate करने से पहले हमें front end यानि HTML Form को JavaScript या predefined attribute की help से भी validate करना चाहिए।
जैसे नीच दिए गए example में 4 inputs को उनके नाम के according validate किया गया है।
<p>Name: <input type="text" name="name" required></p>
<p>E-mail: <input type="email" name="email" required></p>
<p>Mobile No : <input type="text" name="mobileno" required max="10" min="10"></p>
<p>Website: <input type="url" name="website" required></p>
●●●
PHP में कुछ predefined functions और flags हैं जिनकी help से हम किसी external data को validate कर सकते हैं।
isset() : isset() function check करता है कि कोई variable define किया गया है और null
नहीं है। variable declare और null
न होने true
return करता है otherwise false
.
empty() : empty() method check करता है कि क्या कोई variable empty है , empty होने पर true
otherwise false return
करता है।
stripslashes()
: इस function का use किसी string से backslashes को remove करने के लिए किया जाता है।
htmlspecialchars()
: यह function special characters को उसके corresponding HTML entities में convert करता है जैसे <
को <
और >
को &
में।
trim()
: यह string के left और right white spaces remove करता है।
filter_var() : user input data को validate करने के लिए यह function बहुत important है। इस function को different - different Fags
का use करके data को validate
और sanitize
किया जाता है।
●●●
validate करते time input errors को हम एक Array में store कर लेंगे ताकि कोई error आने पर उसे show भी कर सकें।
$errors = [];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// first validate.
if(! filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
$errors["email"] = "Enter valid email address";
}
if(! filter_var($_POST["website"], FILTER_VALIDATE_URL)) {
$errors["website"] = "Enter valid website URL";
}
if(!ctype_digit($_POST["mobileno"]) || strlen($_POST["mobileno"]) != 10) {
$errors["website"] = "Enter valid mobile number";
}
// check errors.
if(count($errors) > 0) {
// redirect them or show errors.
}
else{
// now sanitize data.
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$website = filter_var($_POST["website"], FILTER_SANITIZE_URL);
$comment = $_POST["mobileno"];
// now process data or store in database.
}
}
अगर आप form को same file में submit कर रहे हैं तो $_SERVER["PHP_SELF"]
का use htmlspecialchars()
के साथ ही करें , क्योंकि $_SERVER["PHP_SELF"]
आपको current location return करता है , जिसे modify भी किया जसा सकता है ।
<form action="<?php htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<-- form inputs -->
</form>
जैसे अगर current URL test.com/add.php
है और add.php
की जगह किसी hacker ने कुछ और URL enter किया तो form उस location पर submit होगा।
●●●
I hope , आपको PHP में Form validation के बारे में थोड़ा समझ आ गया होगा।
Loading ...
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook