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

PHP Superglobals

Superglobals PHP द्वारा हर जगह within reach रखे गए built-in boxes of information हैं, किसी school bulletin board जैसे जिसे हर room देख सकता है। इनमें form data, cookies, और visitor के बारे में details जैसी चीज़ें होती हैं।
Syntax
php
$_SERVER["KEY"];      // request and server info
$_GET["key"];
$_POST["key"];
$_SESSION["key"];     // after session_start()
$_COOKIE["key"];
$GLOBALS["variable_name"];

Superglobals क्या हैं?

Superglobals built-in PHP arrays हैं जो हर scope में automatically available हैं — किसी भी function या class method के अंदर — बिना pass किए या global declare किए।

ये current request, server environment, और session के बारे में information carry करने के लिए exist करते हैं जिसकी application के किसी भी हिस्से को ज़रूरत हो सकती है।

उदाहरण: What Are Superglobals?

php
<?php
function showUser() {
    $_SESSION['user'] = "Alice"; // no `global` needed
    echo $_SESSION['user'];
}
session_start();
showUser();
?>

$_SERVER और $GLOBALS

$_SERVER request और server metadata रखता है जैसे requested URL (REQUEST_URI), HTTP method, और headers।

$GLOBALS किसी function के अंदर से global scope में declare किए गए हर variable तक access देता है, जो technically possible है लेकिन आमतौर पर explicit parameters के favor में avoid किया जाता है।

उदाहरण: $_SERVER and $GLOBALS

php
<?php
// Set `$_SERVER['REQUEST_URI']` to '/home'
$_SERVER['REQUEST_URI'] = '/home';
// Print `$_SERVER['REQUEST_URI'] . "\n"` to the output
echo $_SERVER['REQUEST_URI'] . "\n";

// Declare `$name`, set to "Alice"
$name = "Alice";
// Define the function `show` with no parameters
function show() {
    // Print `$GLOBALS['name']` to the output
    echo $GLOBALS['name'];
}
// Call `show()`
show();
?>

$_SESSION और $_COOKIE

$_SESSION उस data को store करता है जो same visitor से कई requests में persist रहता है, जो आमतौर पर एक cookie में stored session ID से backed होता है, और इस्तेमाल से पहले session_start() चाहिए।

$_COOKIE browser द्वारा भेजे गए raw cookie values को सीधे पढ़ता है, बिना किसी server-side session logic के।

उदाहरण: $_SESSION and $_COOKIE

php
<?php
// Call `session_start()`
session_start();
// Set `$_SESSION['user_id']` to `5`
$_SESSION['user_id'] = 5;
// Set `$_COOKIE['theme']` to 'dark'
$_COOKIE['theme'] = 'dark';
// Print `$_SESSION['user_id'] . " " . $_COOKIE['theme']` to the output
echo $_SESSION['user_id'] . " " . $_COOKIE['theme'];
?>

Uploads के लिए $_FILES

$_FILES तब populate होता है जब कोई form enctype="multipart/form-data" के साथ submit होता है, आपको हर uploaded file का temporary path, original name, size, और कोई भी upload error code देते हुए — यह सब कहीं permanent move करने से पहले validate किया जाना चाहिए।

उदाहरण: $_FILES for Uploads

php
<?php
// Set `$_FILES['avatar']` to `['name' => 'pic.png', 'tmp_name' => '/tmp/xyz', 'error' => 0]`
$_FILES['avatar'] = ['name' => 'pic.png', 'tmp_name' => '/tmp/xyz', 'error' => 0];
// Check whether `$_FILES['avatar']['error'] === 0`
if ($_FILES['avatar']['error'] === 0) {
    // Print `"Upload OK: " . $_FILES['avatar']['name']` to the output
    echo "Upload OK: " . $_FILES['avatar']['name'];
}
?>

$_ENV और Security Considerations

$_ENV environment variables expose करता है जिनके साथ web server शुरू हुआ था, जो अक्सर database credentials जैसे secrets के लिए इस्तेमाल होता है ताकि वे कभी source code में न दिखें।

क्योंकि हर superglobal $_ENV और $GLOBALS को छोड़कर untrusted external input reflect करता है, हमेशा उनके contents पर भरोसा करने से पहले validate करें।

उदाहरण: $_ENV and Security Considerations

php
<?php
// Call `putenv("DB_PASSWORD=secret")`
putenv("DB_PASSWORD=secret");
// Print `getenv("DB_PASSWORD") . "\n"` to the output
echo getenv("DB_PASSWORD") . "\n";
// Print "Always validate superglobal input before trusting it" to the output
echo "Always validate superglobal input before trusting it";
?>
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. पहले session_start() call किए बिना $_SESSION इस्तेमाल करने की कोशिश करना, ताकि array खाली हो और values requests के बीच save न हों।
  2. $GLOBALS या variables को किसी function में इस तरह pass करना जैसे वे available न हों, जबकि superglobals को functions के अंदर किसी global keyword की ज़रूरत नहीं।
  3. $_SERVER[HTTP_HOST] या दूसरी request-derived entries को safe मानकर भरोसा करना, जबकि clients इन्हें forge कर सकते हैं।
🔒

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.