Bootstrap FreeKB - PHP - Debugging using var_dump
PHP - Debugging using var_dump

Updated:   |  PHP articles

The var_dump and print_r functions can be used to display the output of a variable. This is often used with built in PHP variables, such as the $_SERVER variable.

<?php
  var_dump($_SERVER);
?>

 

Which should return something like this.

array(34) { ["USER"]=> string(6) "apache" ["HOME"]=> string(16) "/usr/share/httpd" ["HTTP_COOKIE"]=> string(36) "PHPSESSID=fmj2eh6igjs10ads9h75r8hs6b" ["HTTP_ACCEPT_LANGUAGE"]=> string(14) "en-US,en;q=0.9" ["HTTP_ACCEPT_ENCODING"]=> string(13) "gzip, deflate" ["HTTP_ACCEPT"]=> string(135) "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" ["HTTP_USER_AGENT"]=> string(115) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" ["HTTP_UPGRADE_INSECURE_REQUESTS"]=> string(1) "1" ["HTTP_CONNECTION"]=> string(10) "keep-alive" ["HTTP_HOST"]=> string(20) "www.example.com" ["PATH_INFO"]=> string(0) "" ["SCRIPT_FILENAME"]=> string(23) "/var/www/www/Stage1.php" ["REDIRECT_STATUS"]=> string(3) "200" ["SERVER_NAME"]=> string(14) "www.example.com" ["SERVER_PORT"]=> string(2) "80" ["SERVER_ADDR"]=> string(12) "10.5.67.34" ["REMOTE_PORT"]=> string(5) "50496" ["REMOTE_ADDR"]=> string(13) "10.6.23.123" ["SERVER_SOFTWARE"]=> string(12) "nginx/1.20.1" ["GATEWAY_INTERFACE"]=> string(7) "CGI/1.1" ["REQUEST_SCHEME"]=> string(4) "http" ["SERVER_PROTOCOL"]=> string(8) "HTTP/1.1" ["DOCUMENT_ROOT"]=> string(12) "/var/www/www" ["DOCUMENT_URI"]=> string(11) "/Stage1.php" ["REQUEST_URI"]=> string(11) "/Stage1.php" ["SCRIPT_NAME"]=> string(11) "/Stage1.php" ["CONTENT_LENGTH"]=> string(0) "" ["CONTENT_TYPE"]=> string(0) "" ["REQUEST_METHOD"]=> string(3) "GET" ["QUERY_STRING"]=> string(0) "" ["FCGI_ROLE"]=> string(9) "RESPONDER" ["PHP_SELF"]=> string(11) "/Stage1.php" ["REQUEST_TIME_FLOAT"]=> float(1626683094.0901) ["REQUEST_TIME"]=> int(1626683094) }

 

Or, specific elements can be printed, like this.

echo "Server Port = " . $_SERVER[SERVER_PORT] . "<br />";

 

Which will return the following.

Server Port = 80

 

Or you can loop through the array and print the keys and values.

foreach ($_SERVER as $key => $value) {
  echo $key . ' = ' . $value . '<br \>';
}

 

Which should return the following.

USER = apache
HOME = /usr/share/httpd
HTTP_COOKIE = PHPSESSID=fmj2eh6igjs10ads9h75r8hs6b
HTTP_ACCEPT_LANGUAGE = en-US,en;q=0.9
HTTP_ACCEPT_ENCODING = gzip, deflate
HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
HTTP_USER_AGENT = Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
HTTP_UPGRADE_INSECURE_REQUESTS = 1
HTTP_CACHE_CONTROL = max-age=0
HTTP_CONNECTION = keep-alive
HTTP_HOST = www.example.com
PATH_INFO =
SCRIPT_FILENAME = /var/www/www/Stage1.php
REDIRECT_STATUS = 200
SERVER_NAME = www.freekb.net
SERVER_PORT = 80
SERVER_ADDR = 10.5.89.231
REMOTE_PORT = 56292
REMOTE_ADDR = 10.17.6.34
SERVER_SOFTWARE = nginx/1.20.1
GATEWAY_INTERFACE = CGI/1.1
REQUEST_SCHEME = http
SERVER_PROTOCOL = HTTP/1.1
DOCUMENT_ROOT = /var/www/www
DOCUMENT_URI = /Stage1.php
REQUEST_URI = /Stage1.php
SCRIPT_NAME = /Stage1.php
CONTENT_LENGTH =
CONTENT_TYPE =
REQUEST_METHOD = GET
QUERY_STRING =
FCGI_ROLE = RESPONDER
PHP_SELF = /Stage1.php
REQUEST_TIME_FLOAT = 1626684305.9486
REQUEST_TIME = 1626684305

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 81e82e in the box below so that we can be sure you are a human.