PHP User Input (HTML Forms)
In this page:
<form action="script.php" method="POST">
<input type="text" name="field_name">
<button type="submit">Send</button>
</form>
<?php
if (isset($_POST["field_name"])) {
echo $_POST["field_name"];
}
?>
HTML Forms और PHP
जब एक HTML <form> submit होता है, browser हर named input की value package करता है और इसे उस URL पर भेजता है जो form का action attribute point करता है, form द्वारा specify किए गए method (GET या POST) का इस्तेमाल करते हुए।
उस URL पर चल रहा PHP फिर corresponding superglobal array से submitted values पढ़ता है।
उदाहरण: HTML Forms and PHP
<!DOCTYPE html>
<html>
<body>
<form action="process.php" method="POST">
<input type="text" name="username">
</form>
<?php
// PHP reads the submitted value from $_POST['username']
?>
</body>
</html>
Login to try C/C++/Java/PHP code in the editor
Simple Form Text Handle करना
$_POST और $_GET associative arrays हैं जहाँ हर key किसी HTML input के name attribute से match करती है।
एक live form में, PHP हर request पर इन्हें automatically populate करता है; सीखते समय, मैन्युअली $_POST में values assign करके एक submission simulate करना normal है ताकि आप बिना browser के handling logic test कर सकें।
उदाहरण: Handling Simple Form Text
<?php
// Simulating a form submission for testing
$_POST['username'] = "Alice";
echo $_POST['username'];
?>
Login to try C/C++/Java/PHP code in the editor
Form Submit हुआ या नहीं Check करना
जब कोई form actually submit ही नहीं हुआ तब $_POST[email] पढ़ना एक 'undefined array key' warning trigger करता है।
पहले isset($_POST[email]) check करना, या $_SERVER[REQUEST_METHOD] inspect करना, आपकी script को एक genuine submission को पहली बार page visit से safely अलग करने देता है।
उदाहरण: Checking if Form is Submitted
<?php
// Check whether `isset($_POST['email'])`
if (isset($_POST['email'])) {
// Print `"Form submitted with: " . $_POST['email']` to the output
echo "Form submitted with: " . $_POST['email'];
// Otherwise, run this branch
} else {
// Print "No form submitted yet." to the output
echo "No form submitted yet.";
}
?>
Login to try C/C++/Java/PHP code in the editor
Form Input Sanitization
$_GET, $_POST, या $_COOKIE से आई कोई भी value आपके control के बाहर से आती है और इसे untrusted माना जाना चाहिए।
htmlspecialchars() के बिना raw input को HTML output में pass करना एक attacker को एक <script> tag inject करने देता है जो किसी दूसरे user के browser में चलता है — एक classic Cross-Site Scripting (XSS) vulnerability।
उदाहरण: Form Input Sanitization
<?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
Missing Inputs के लिए Default Values
एक ऐसी form key access करना जो कभी submit ही नहीं हुई (मान लीजिए एक unchecked checkbox) चुपचाप कुछ न return करने के बजाय एक warning throw करता है।
$_POST[newsletter] ?? false key के missing होते ही एक sensible default पर fall back करके इससे बच जाता है, किसी isset() check की ज़रूरत नहीं।
उदाहरण: Default Values for Missing Inputs
<?php
// Declare `$newsletter`, set to `$_POST['newsletter'] ?? false`
$newsletter = $_POST['newsletter'] ?? false;
// Print a detailed dump (with types) of `$newsletter`
var_dump($newsletter);
?>
Login to try C/C++/Java/PHP code in the editor
- form submit हुआ है यह check किए बिना
$_POST[email]पढ़ना, जो पहले page load पर एक 'Undefined array key' warning trigger करता है। htmlspecialchars()के बिनाecho $_POST[name];से submitted values print करना, जो cross-site scripting (XSS) की अनुमति देता है।- किसी
<input>परnameattribute भूल जाना, ताकि field कभी भेजी न जाए और$_POSTमें न दिखे।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: