PHP Form Handling
In this page:
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$value = $_POST["field_name"];
}
$value = $_GET["field_name"];
GET Forms Handle करना
एक HTML form का data $_GET या $_POST superglobal array के through PHP तक पहुँचता है, form के method attribute के आधार पर, हर array key corresponding input field के name attribute से match करते हुए।
उदाहरण: Handling GET Forms
<?php
// Set `$_GET['search']` to "php tutorials"
$_GET['search'] = "php tutorials";
// Print `$_GET['search']` to the output
echo $_GET['search'];
?>
Login to try C/C++/Java/PHP code in the editor
POST Forms Handle करना
किसी script के top पर $_SERVER[REQUEST_METHOD] === POST check करना यह detect करने का standard तरीका है कि form actually submit हुआ या नहीं, बनाम page सिर्फ पहली बार normally load हो रहा हो।
उदाहरण: Handling POST Forms
<?php
// Set `$_SERVER['REQUEST_METHOD']` to 'POST'
$_SERVER['REQUEST_METHOD'] = 'POST';
// Check whether `$_SERVER['REQUEST_METHOD'] === 'POST'`
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Print "Form was submitted" to the output
echo "Form was submitted";
// Otherwise, run this branch
} else {
// Print "Just a normal page load" to the output
echo "Just a normal page load";
}
?>
Login to try C/C++/Java/PHP code in the editor
Form Submission Validate करना
$_POST या $_GET से आने वाली हर value को untrusted user input माना जाना चाहिए — इसे database query, file path, या displayed HTML में इस्तेमाल करने से पहले हमेशा इसका type और content validate करें।
उदाहरण: Validating Form Submission
<?php
// Set `$_POST['age']` to "25"
$_POST['age'] = "25";
// Declare `$age`, set to `$_POST['age']`
$age = $_POST['age'];
// Check whether `is_numeric($age) && $age > 0`
if (is_numeric($age) && $age > 0) {
// Print "Valid age: $age" to the output
echo "Valid age: $age";
}
?>
Login to try C/C++/Java/PHP code in the editor
Multi-value Inputs Process करना
htmlspecialchars() को किसी भी user-submitted value को HTML में वापस echo करने से पहले उसे wrap करना चाहिए, किसी दूसरे visitor के browser में execute होने से किसी form field में type किए गए malicious script tag को रोकते हुए (cross-site scripting)।
उदाहरण: Processing Multi-value Inputs
<?php
// Set `$_POST['comment']` to "<script>alert('xss')</script>"
$_POST['comment'] = "<script>alert('xss')</script>";
// Print `htmlspecialchars($_POST['comment'])` to the output
echo htmlspecialchars($_POST['comment']);
?>
Login to try C/C++/Java/PHP code in the editor
Empty Inputs Check करना
एक successful form submission के बाद redirect करना (Post/Redirect/Get pattern) browser को उसी form data को दोबारा submit करने से रोकता है अगर user resulting page refresh कर दे।
उदाहरण: Checking Empty Inputs
<?php
// After a successful POST, redirecting avoids duplicate submissions on refresh
$submitted = true;
if ($submitted) {
echo "header('Location: /thank-you.php'); // Post/Redirect/Get pattern";
}
?>
Login to try C/C++/Java/PHP code in the editor
- जब form
method="get"इस्तेमाल करता है तब$_POSTपढ़ना, या इसका उल्टा। - बिना validate या escape किए user input पर भरोसा करना।
- form fields पर
nameattributes भूल जाना, ताकि वे भेजे ही न जाएँ।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: