← Back to PHP Course | Chapter 2: Output & Input | Lesson 4 of 8

PHP $_GET और $_POST

GET और POST दो तरीके हैं जिनसे एक form information भेज सकता है। GET अपना message एक postcard पर लिखने जैसा है जिसे कोई भी पढ़ सकता है, जबकि POST इसे एक envelope में seal करने जैसा है।
Syntax
php
$_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
<?php
// Set `$_GET['search']` to "php"
$_GET['search'] = "php";
// Print `"Search query: " . $_GET['search']` to the output
echo "Search query: " . $_GET['search'];
?>

$_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
<?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";
?>

$_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
<?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'];
?>

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
<?php
$_GET['id'] = "6"; // user could edit this in the address bar
$id = (int) $_GET['id'];
echo "Validated id: " . $id;
?>

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
<?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.";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. $_GET से passwords या दूसरा sensitive data भेजना, जो उन्हें URL, browser history और server logs में डाल देता है।
  2. किसी database query में सीधे $_GET[id] पर भरोसा करना, जो SQL injection की अनुमति देता है; इसे validate करें और prepared statements इस्तेमाल करें।
  3. form में method="post" set करना लेकिन $_GET पढ़ना, या इसका उल्टा, ताकि expected array खाली हो।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.