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

PHP $_REQUEST

request array एक mailbox जैसा है जहाँ सारे incoming messages एक साथ आते हैं, चाहे वे किसी भी तरीके से भेजे गए हों। यह handy है, लेकिन आप यह नहीं बता सकते कि हर delivery किस method से आई।
Syntax
php
$_REQUEST["key"];   // combined $_GET, $_POST and $_COOKIE data

$_REQUEST क्या है?

$_REQUEST $_GET, $_POST, और $_COOKIE के contents को एक single array में merge कर देता है, इसलिए $_REQUEST से पढ़ने वाली एक script को पहले से जानने की ज़रूरत नहीं कि data कौन सा method लाया — एक quick prototype के लिए convenient, एक बार आप explicit होने की परवाह करें तो कम।

उदाहरण: What is $_REQUEST?

php
<?php
// Set `$_GET['q']` to "search term"
$_GET['q'] = "search term";
// Print `$_REQUEST['q']` to the output
echo $_REQUEST['q'];
?>

$_REQUEST के through GET Data पढ़ना

URL query string के through actually भेजी गई किसी value के लिए $_REQUEST[q] पढ़ना exactly वैसे ही काम करता है जैसे इसे $_GET[q] से पढ़ना करेगा — $_REQUEST बस $_GET के contents को बाकियों के साथ fold कर देता है, एक catch-all fallback की तरह act करते हुए।

उदाहरण: Reading GET Data via $_REQUEST

php
<?php
// Set `$_GET['q']` to "php tutorials"
$_GET['q'] = "php tutorials";
// Print `$_REQUEST['q']` to the output
echo $_REQUEST['q'];
?>

$_REQUEST के through POST Data पढ़ना

POST से भेजे गए form data के लिए भी वही लागू होता है: $_REQUEST[username] इसे उसी तरह पकड़ता है जैसे $_POST[username] करता।

Convenience यह है कि आपके code को branch नहीं करना पड़ता कि कौन सा method इस्तेमाल हुआ — tradeoff यह है कि यह सिर्फ $_REQUEST देखकर यह भी नहीं बता सकता कि कौन सा method इस्तेमाल हुआ।

उदाहरण: Reading POST Data via $_REQUEST

php
<?php
// Set `$_POST['username']` to "Alice"
$_POST['username'] = "Alice";
// Print `$_REQUEST['username']` to the output
echo $_REQUEST['username'];
?>

_REQUEST में Variables Precedence

अगर एक GET parameter और एक POST parameter same key share करते हैं, तो $_REQUEST के अंदर कौन जीतता है यह php.ini में PHP की request_order (या पुरानी variables_order) setting decide करती है — आपके code से नहीं — जो outcome को खुद script में दिखने वाली किसी चीज़ के बजाय server configuration पर depend करा देता है।

उदाहरण: Variables Precedence in _REQUEST

php
<?php
$_GET['key'] = "from-get";
$_POST['key'] = "from-post";
// Which one $_REQUEST['key'] holds depends on php.ini's request_order setting
echo $_REQUEST['key'];
?>

Limitations और Security

क्योंकि $_REQUEST एक GET request को एक POST से अलग नहीं बता सकता, इस पर भरोसा करने वाला code एक real form submission के बजाय एक simple crafted URL के through एक state-changing action accept करने के लिए trick किया जा सकता है।

Explicitly $_GET या $_POST इस्तेमाल करना इस पूरी class की confusion से बचाता है।

उदाहरण: Limitations and Security

php
<?php
// A crafted URL like ?delete_account=1 could trigger this if code trusts $_REQUEST
$_GET['delete_account'] = 1;
if (isset($_REQUEST['delete_account'])) {
    echo "This should require $_POST, not any $_REQUEST source.";
}
?>
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. $_REQUEST इस्तेमाल करना जब आप जानते हैं कि data POST से आता है, जो एक attacker को वह value URL या एक cookie के through भी supply करने देता है।
  2. $_REQUEST पर भरोसा करना जब एक GET और एक POST parameter same key share करते हैं, क्योंकि कौन जीतता है यह php.ini में request_order setting पर depend करता है।
  3. यह मान लेना कि $_REQUEST में हर server पर cookie data शामिल है, जबकि modern default configuration में सिर्फ GET और POST शामिल हैं।
🔒

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.