0
0
PHPprogramming~10 mins

$_SERVER information in PHP - Interactive Code Practice

Choose your learning style9 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.