PHP var_export() Function In Hindi | var_export() In PHP In Hindi
Selection Sort in PHP With Example | How does selection sort work in PHP ?
PHP stdClass In Hindi | stdClass In PHP
PHP Interview Questions In Hindi : PHP Interview Preparation
PHP Error Control Operator In Hindi | Error Control In PHP
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.
किसी भी तरह के 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 ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook