The $_SERVER variable holds information about headers, paths, and script locations. It helps you learn about the current request and server environment.
To find out the user's IP address visiting your website.
To get the current page URL or script name.
To check which browser or device the visitor is using.
To know the server software or version running your site.
To handle redirects or links based on the current domain.
Syntax
PHP
$_SERVER['KEY_NAME'];
Replace KEY_NAME with the specific information you want, like REMOTE_ADDR for IP address.
$_SERVER is a superglobal array, so you can access it anywhere in your PHP script.
Examples
Prints the visitor's IP address.
PHP
echo $_SERVER['REMOTE_ADDR'];
Shows the browser or device details of the visitor.
PHP
echo $_SERVER['HTTP_USER_AGENT'];
Displays the path of the current script.
PHP
echo $_SERVER['SCRIPT_NAME'];
Sample Program
This program prints the visitor's IP, the current script path, and browser details using $_SERVER.
PHP
<?php
// Show some server infoecho"Your IP address is: " . $_SERVER['REMOTE_ADDR'] . "\n";
echo"You are visiting: " . $_SERVER['SCRIPT_NAME'] . "\n";
echo"Your browser info: " . $_SERVER['HTTP_USER_AGENT'] . "\n";
?>
OutputSuccess
Important Notes
Some $_SERVER keys may not be available depending on the server or request type.
Never trust $_SERVER data blindly; it can be spoofed by users.
Summary
$_SERVER holds useful info about the visitor and server.
You access it like an array with keys for different details.
It helps customize responses based on who visits and how.
Practice
(1/5)
1. What type of data does the PHP $_SERVER superglobal array hold?
easy
A. User input from forms
B. Information about headers, paths, and script locations
C. Database connection details
D. Session variables
Solution
Step 1: Understand what $_SERVER contains
$_SERVER holds server and execution environment information like headers and script paths.
Step 2: Compare with other options
User input is in $_POST or $_GET, database details are not stored here, and session variables are in $_SESSION.
Final Answer:
Information about headers, paths, and script locations -> Option B
Quick Check:
$_SERVER = server info [OK]
Hint: Remember: $_SERVER = server and request info [OK]
Common Mistakes:
Confusing $_SERVER with $_POST or $_GET
Thinking $_SERVER holds user session data
Assuming $_SERVER contains database info
2. Which of the following is the correct way to get the current script's filename using $_SERVER?
easy
A. $_SERVER['SCRIPT_NAME']
B. $_SERVER['USER_AGENT']
C. $_SERVER['REMOTE_ADDR']
D. $_SERVER['HTTP_HOST']
Solution
Step 1: Identify the key for current script filename
$_SERVER['SCRIPT_NAME'] returns the path of the current script.
Step 2: Understand other keys
USER_AGENT is client browser info, REMOTE_ADDR is client IP, and HTTP_HOST is the host name.
Final Answer:
$_SERVER['SCRIPT_NAME'] -> Option A
Quick Check:
Script filename = $_SERVER['SCRIPT_NAME'] [OK]
Hint: SCRIPT_NAME gives current script path [OK]
Common Mistakes:
Using REMOTE_ADDR for filename
Confusing HTTP_HOST with script name
Trying USER_AGENT for script info
3. What will the following PHP code output if the visitor's IP address is 192.168.1.10?
echo $_SERVER['REMOTE_ADDR'];
medium
A. 192.168.1.10
B. The server's IP address
C. The visitor's browser name
D. An empty string
Solution
Step 1: Understand what REMOTE_ADDR holds
$_SERVER['REMOTE_ADDR'] contains the IP address of the client visiting the page.
Step 2: Match with given IP
The visitor's IP is 192.168.1.10, so that is what will be printed.
Final Answer:
192.168.1.10 -> Option A
Quick Check:
REMOTE_ADDR = visitor IP [OK]
Hint: REMOTE_ADDR shows visitor IP address [OK]
Common Mistakes:
Thinking REMOTE_ADDR is server IP
Confusing REMOTE_ADDR with HTTP_USER_AGENT
Expecting browser name instead of IP
4. Identify the error in this PHP code snippet that tries to print the visitor's browser info:
echo $_SERVER[HTTP_USER_AGENT];
medium
A. $_SERVER cannot be echoed directly
B. Using wrong key name
C. Missing quotes around the array key
D. HTTP_USER_AGENT is not available in $_SERVER
Solution
Step 1: Check array key syntax
Array keys must be strings in quotes. Here, HTTP_USER_AGENT is missing quotes.
Step 2: Confirm key existence
HTTP_USER_AGENT is a valid key in $_SERVER, so the key name is correct.
Final Answer:
Missing quotes around the array key -> Option C
Quick Check:
Array keys need quotes [OK]
Hint: Always quote $_SERVER keys like 'HTTP_USER_AGENT' [OK]
Common Mistakes:
Forgetting quotes around keys
Assuming key name is wrong
Trying to echo whole $_SERVER array directly
5. You want to create a PHP script that shows the visitor's IP address and the current script's filename. Which code snippet correctly does this?