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

PHP $_SERVER

हर incoming web request सिर्फ submitted form data से कहीं ज़्यादा information carry करता है -- कौन सा URL request किया गया, कौन सा HTTP method इस्तेमाल हुआ, visitor का IP address क्या है, कौन सा server request handle कर रहा है। PHP इस सब को $_SERVER superglobal array में collect कर देता है, scripts को request और server environment details तक आसान access देते हुए।
Syntax
php
$_SERVER["REQUEST_METHOD"];   // GET, POST, ...
$_SERVER["REQUEST_URI"];
$_SERVER["HTTP_HOST"];
$_SERVER["REMOTE_ADDR"];

Basic Request Information पढ़ना

$_SERVER["REQUEST_METHOD"] आपको बताता है कि current request GET, POST, या कोई और HTTP method था, और $_SERVER["REQUEST_URI"] आपको exact path (और query string) देता है जो request किया गया था -- दो सबसे commonly इस्तेमाल होने वाली $_SERVER keys।

उदाहरण: Reading Basic Request Information

php
<?php
// Set `$_SERVER["REQUEST_METHOD"]` to "GET"
$_SERVER["REQUEST_METHOD"] = "GET";
// Set `$_SERVER["REQUEST_URI"]` to "/products?id=5"
$_SERVER["REQUEST_URI"] = "/products?id=5";
// Print `$_SERVER["REQUEST_METHOD"] . " " . $_SERVER["REQUEST_URI"]` to the output
echo $_SERVER["REQUEST_METHOD"] . " " . $_SERVER["REQUEST_URI"];
?>

Server और Host Identify करना

$_SERVER["HTTP_HOST"] वह domain name देता है जो visitor ने site तक पहुँचने के लिए इस्तेमाल किया, और $_SERVER["SERVER_NAME"] server का अपना configured name देता है -- ये absolute URLs बनाने या (एक multi-domain setup में) यह पता लगाने के लिए उपयोगी हैं कि किस domain ने current request serve किया।

उदाहरण: Identifying the Server and Host

php
<?php
// Set `$_SERVER["HTTP_HOST"]` to "example.com"
$_SERVER["HTTP_HOST"] = "example.com";
// Set `$_SERVER["SERVER_NAME"]` to "example.com"
$_SERVER["SERVER_NAME"] = "example.com";
// Print `$_SERVER["HTTP_HOST"] . " / " . $_SERVER["SERVER_NAME"]` to the output
echo $_SERVER["HTTP_HOST"] . " / " . $_SERVER["SERVER_NAME"];
?>

Visitor का IP Address पढ़ना

$_SERVER["REMOTE_ADDR"] वह IP address रखता है जिससे server request आते देखता है -- logging, rate limiting, या geolocation lookups के लिए उपयोगी, हालाँकि यह किसी specific visitor को uniquely identify करने का पूरी तरह reliable तरीका नहीं है।

उदाहरण: Reading the Visitor's IP Address

php
<?php
// Set `$_SERVER["REMOTE_ADDR"]` to "192.168.1.10"
$_SERVER["REMOTE_ADDR"] = "192.168.1.10";
// Print `"Request came from: " . $_SERVER["REMOTE_ADDR"]` to the output
echo "Request came from: " . $_SERVER["REMOTE_ADDR"];
?>

$_SERVER के through HTTP Headers पढ़ना

कई HTTP request headers $_SERVER के through एक HTTP_ prefix और header name को uppercase करके underscores के साथ expose होते हैं -- $_SERVER["HTTP_USER_AGENT"] browser की user-agent string रखता है, और $_SERVER["HTTP_REFERER"] वह page रखता है जहाँ से visitor आया, अगर कोई हो।

उदाहरण: Reading HTTP Headers via $_SERVER

php
<?php
// Set `$_SERVER["HTTP_USER_AGENT"]` to "Mozilla/5.0"
$_SERVER["HTTP_USER_AGENT"] = "Mozilla/5.0";
// Set `$_SERVER["HTTP_REFERER"]` to "https://google.com"
$_SERVER["HTTP_REFERER"] = "https://google.com";
// Print `$_SERVER["HTTP_USER_AGENT"] . "\n" . $_SERVER["HTTP_REFERER"]` to the output
echo $_SERVER["HTTP_USER_AGENT"] . "\n" . $_SERVER["HTTP_REFERER"];
?>

Common $_SERVER Keys Reference

पहले से covered keys के अलावा, कुछ और अक्सर आती हैं: SCRIPT_NAME (currently executing script का path), DOCUMENT_ROOT (server की web root directory), SERVER_PROTOCOL (इस्तेमाल हुआ HTTP version), और QUERY_STRING (URL में ? के बाद की हर चीज़)।

उदाहरण: Common $_SERVER Keys Reference

php
<?php
// Set `$_SERVER["SCRIPT_NAME"]` to "/index.php"
$_SERVER["SCRIPT_NAME"] = "/index.php";
// Set `$_SERVER["DOCUMENT_ROOT"]` to "/var/www/html"
$_SERVER["DOCUMENT_ROOT"] = "/var/www/html";
// Set `$_SERVER["QUERY_STRING"]` to "id=5"
$_SERVER["QUERY_STRING"] = "id=5";
// Print `$_SERVER["SCRIPT_NAME"] . " " . $_SERVER["DOCUMENT_ROOT"] . " " . $_SERVER["QUERY_STRING"]` to the output
echo $_SERVER["SCRIPT_NAME"] . " " . $_SERVER["DOCUMENT_ROOT"] . " " . $_SERVER["QUERY_STRING"];
?>
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. client से आने वाली $_SERVER values (जैसे HTTP_USER_AGENT या HTTP_REFERER) पर ऐसे भरोसा करना जैसे वे guaranteed accurate हों -- browsers और clients इन headers के लिए जो चाहें वह values भेज सकते हैं।
  2. $_SERVER["REMOTE_ADDR"] को किसी unique visitor को identify करने का foolproof तरीका मानना, जबकि proxies, shared networks, और VPNs का मतलब है कि कई visitors एक IP share कर सकते हैं।
  3. यह भूल जाना कि हर $_SERVER key हर server setup पर exist करने की guarantee नहीं है -- किसी less-common key पर भरोसा करने से पहले हमेशा isset() से check करें।
चैप्टर सारांश
  • $_SERVER एक superglobal array है जो current request और उसे handle कर रहे server environment के बारे में information रखता है।
  • Common keys में REQUEST_METHOD, REQUEST_URI, HTTP_HOST, REMOTE_ADDR, और SCRIPT_NAME शामिल हैं।
  • Client-supplied $_SERVER values (जैसे HTTP headers) पर security-sensitive decisions के लिए कभी blindly भरोसा नहीं किया जाना चाहिए।
🔒

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.