PHP Superglobals
In this page:
$_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
function showUser() {
$_SESSION['user'] = "Alice"; // no `global` needed
echo $_SESSION['user'];
}
session_start();
showUser();
?>
Login to try C/C++/Java/PHP code in the editor
$_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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
$_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
// 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'];
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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'];
}
?>
Login to try C/C++/Java/PHP code in the editor
$_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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
- पहले
session_start()call किए बिना$_SESSIONइस्तेमाल करने की कोशिश करना, ताकि array खाली हो और values requests के बीच save न हों। $GLOBALSया variables को किसी function में इस तरह pass करना जैसे वे available न हों, जबकि superglobals को functions के अंदर किसीglobalkeyword की ज़रूरत नहीं।$_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: