PHP $_GET और $_POST
In this page:
$_GET["key"]; // from the URL query string: page.php?key=value
$_POST["key"]; // from the request body of a submitted form
$_GET को समझना
$_GET URL में एक query string (?search=php) की तरह appended values collect करता है, जिसका मतलब है कि data address bar, browser history, और server logs में visible है।
वह visibility search results जैसे shareable, bookmarkable pages के लिए एक feature है — और किसी भी sensitive चीज़ के लिए एक liability।
उदाहरण: Understanding $_GET
<?php
// Set `$_GET['search']` to "php"
$_GET['search'] = "php";
// Print `"Search query: " . $_GET['search']` to the output
echo "Search query: " . $_GET['search'];
?>
Login to try C/C++/Java/PHP code in the editor
$_POST को समझना
$_POST अपना data URL के बजाय HTTP request body में carry करता है, इसलिए यह कभी address bar में नहीं दिखता या browser history में cache नहीं होता।
यही वजह है कि login forms, payment forms, और sensitive data handle करने वाली कोई भी दूसरी चीज़ लगभग हमेशा GET के बजाय POST इस्तेमाल करती है।
उदाहरण: Understanding $_POST
<?php
// Set `$_POST['password']` to "secret123"
$_POST['password'] = "secret123";
// Print "Password never appears in the URL: " . strlen($_POST['password']) . " chars" to the output
echo "Password never appears in the URL: " . strlen($_POST['password']) . " chars";
?>
Login to try C/C++/Java/PHP code in the editor
$_GET बनाम $_POST कब इस्तेमाल करें
एक rule of thumb के रूप में: उन requests के लिए $_GET इस्तेमाल करें जो सिर्फ data *पढ़ते* हैं और bookmark या reload करने के लिए safe हैं (search filters, pagination)।
किसी भी ऐसी चीज़ के लिए $_POST इस्तेमाल करें जो server state *बदलती* है — एक account बनाना, एक order place करना — जहाँ एक accidental page reload चुपचाप action को दोहराए नहीं।
उदाहरण: When to Use $_GET vs $_POST
<?php
// $_GET for reading/searching -- safe to bookmark
$_GET['page'] = 2;
// $_POST for actions that change state
$_POST['create_account'] = true;
echo "GET page: " . $_GET['page'];
?>
Login to try C/C++/Java/PHP code in the editor
Safe $_GET Parameters
क्योंकि $_GET values URL में रहती हैं, request आपके server तक पहुँचने से पहले ही एक user इन्हें सीधे address bar में edit कर सकता है — ?id=5 बिना किसी effort के ?id=6 बन जाता है।
कभी यह मान न लें कि एक $_GET value वही है जो आपके अपने links ने generate की थी; इसे exactly वैसे ही validate और sanitize करें जैसे किसी दूसरे user input को।
उदाहरण: Safe $_GET Parameters
<?php
$_GET['id'] = "6"; // user could edit this in the address bar
$id = (int) $_GET['id'];
echo "Validated id: " . $id;
?>
Login to try C/C++/Java/PHP code in the editor
POST Forms Process करना
किसी $_POST submission पर act करने से पहले, $_SERVER[REQUEST_METHOD] === POST confirm करें (या check करें कि expected keys असल में set हैं)।
यह check skip करने का मतलब है कि एक form submission की उम्मीद करने वाली script एक plain page load पर misbehave कर सकती है — या warnings throw कर सकती है।
उदाहरण: Processing 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 "Processing the submitted form." to the output
echo "Processing the submitted form.";
}
?>
Login to try C/C++/Java/PHP code in the editor
$_GETसे passwords या दूसरा sensitive data भेजना, जो उन्हें URL, browser history और server logs में डाल देता है।- किसी database query में सीधे
$_GET[id]पर भरोसा करना, जो SQL injection की अनुमति देता है; इसे validate करें और prepared statements इस्तेमाल करें। - form में
method="post"set करना लेकिन$_GETपढ़ना, या इसका उल्टा, ताकि expected array खाली हो।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: