Bird
Raised Fist0
PHPprogramming~10 mins

$_SERVER information in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the current script's filename using the $_SERVER superglobal.

PHP
<?php
 echo $_SERVER[[1]];
?>
Drag options to blanks, or click blank then click option'
A'SCRIPT_NAME'
B'SERVER_NAME'
C'HTTP_HOST'
D'REQUEST_METHOD'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'SERVER_NAME' instead of 'SCRIPT_NAME' which gives the server's hostname.
Using 'HTTP_HOST' which is the HTTP Host header.
Using 'REQUEST_METHOD' which is the HTTP method used.
2fill in blank
medium

Complete the code to get the client's IP address from the $_SERVER array.

PHP
<?php
 $ip = $_SERVER[[1]];
 echo $ip;
?>
Drag options to blanks, or click blank then click option'
A'REQUEST_URI'
B'SERVER_ADDR'
C'REMOTE_ADDR'
D'HTTP_USER_AGENT'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'SERVER_ADDR' which is the server's IP address.
Using 'HTTP_USER_AGENT' which is the client's browser info.
Using 'REQUEST_URI' which is the requested page URI.
3fill in blank
hard

Fix the error in the code to correctly print the request method used by the client.

PHP
<?php
 echo $_SERVER[[1]];
?>
Drag options to blanks, or click blank then click option'
A'REQUEST_METHOD'
B'REQUESTMETHOD'
C'METHOD_REQUEST'
D'HTTP_METHOD'
Attempts:
3 left
💡 Hint
Common Mistakes
Using keys without underscore or wrong order of words.
Using keys that do not exist in $_SERVER.
4fill in blank
hard

Fill both blanks to create an associative array with the client's browser info and the server's software.

PHP
<?php
 $info = [
   'browser' => $_SERVER[[1]],
   'server' => $_SERVER[[2]]
 ];
 print_r($info);
?>
Drag options to blanks, or click blank then click option'
A'HTTP_USER_AGENT'
B'SERVER_SOFTWARE'
C'HTTP_HOST'
D'REMOTE_ADDR'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up client and server keys.
Using keys that do not exist or are unrelated.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each key in $_SERVER to its length if the length is greater than 10.

PHP
<?php
 $lengths = array_filter(array_map(function($k) {
     return strlen($k);
 }, array_keys($_SERVER)), function($len) {
     return $len [1] 10;
 });

 $result = [];
 foreach ($_SERVER as [2] => [3]) {
     if (strlen([2]) > 10) {
         $result[[2]] = strlen([2]);
     }
 }

 print_r($result);
?>
Drag options to blanks, or click blank then click option'
A>
B$key
C$value
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong filtering.
Swapping key and value variable names.
Using undefined variables inside the loop.

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

  1. Step 1: Understand what $_SERVER contains

    $_SERVER holds server and execution environment information like headers and script paths.
  2. 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.
  3. Final Answer:

    Information about headers, paths, and script locations -> Option B
  4. 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

  1. Step 1: Identify the key for current script filename

    $_SERVER['SCRIPT_NAME'] returns the path of the current script.
  2. Step 2: Understand other keys

    USER_AGENT is client browser info, REMOTE_ADDR is client IP, and HTTP_HOST is the host name.
  3. Final Answer:

    $_SERVER['SCRIPT_NAME'] -> Option A
  4. 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

  1. Step 1: Understand what REMOTE_ADDR holds

    $_SERVER['REMOTE_ADDR'] contains the IP address of the client visiting the page.
  2. Step 2: Match with given IP

    The visitor's IP is 192.168.1.10, so that is what will be printed.
  3. Final Answer:

    192.168.1.10 -> Option A
  4. 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

  1. Step 1: Check array key syntax

    Array keys must be strings in quotes. Here, HTTP_USER_AGENT is missing quotes.
  2. Step 2: Confirm key existence

    HTTP_USER_AGENT is a valid key in $_SERVER, so the key name is correct.
  3. Final Answer:

    Missing quotes around the array key -> Option C
  4. 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?
hard
A.
echo 'IP: ' . $_SERVER['REMOTE_HOST'] . ', Script: ' . $_SERVER['SCRIPT_FILENAME'];
B.
echo 'IP: ' . $_SERVER['SCRIPT_NAME'] . ', Script: ' . $_SERVER['REMOTE_ADDR'];
C.
echo 'IP: ' . $_SERVER['HTTP_HOST'] . ', Script: ' . $_SERVER['REMOTE_ADDR'];
D.
echo 'IP: ' . $_SERVER['REMOTE_ADDR'] . ', Script: ' . $_SERVER['SCRIPT_NAME'];

Solution

  1. Step 1: Identify keys for IP and script filename

    REMOTE_ADDR is visitor IP, SCRIPT_NAME is current script path.
  2. Step 2: Check code correctness

    echo 'IP: ' . $_SERVER['REMOTE_ADDR'] . ', Script: ' . $_SERVER['SCRIPT_NAME'];
    correctly concatenates IP and script name.
    echo 'IP: ' . $_SERVER['SCRIPT_NAME'] . ', Script: ' . $_SERVER['REMOTE_ADDR'];
    swaps them incorrectly.
    echo 'IP: ' . $_SERVER['HTTP_HOST'] . ', Script: ' . $_SERVER['REMOTE_ADDR'];
    uses HTTP_HOST (server host) instead of IP.
    echo 'IP: ' . $_SERVER['REMOTE_HOST'] . ', Script: ' . $_SERVER['SCRIPT_FILENAME'];
    uses REMOTE_HOST (often empty) and SCRIPT_FILENAME (full path, not filename).
  3. Final Answer:

    echo 'IP: ' . $_SERVER['REMOTE_ADDR'] . ', Script: ' . $_SERVER['SCRIPT_NAME']; -> Option D
  4. Quick Check:

    IP = REMOTE_ADDR, Script = SCRIPT_NAME [OK]
Hint: Use REMOTE_ADDR for IP and SCRIPT_NAME for script path [OK]
Common Mistakes:
  • Mixing up REMOTE_ADDR and SCRIPT_NAME
  • Using HTTP_HOST instead of REMOTE_ADDR
  • Using REMOTE_HOST which may be empty
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard