Show PHP Settings with phpinfo and Example PHP $_SERVER Display Page

View PHP Environment Configuration Settings and Superglobals on a Page

PHP is a great computer and web site scripting language and extremely popular. It is used primarily for developing interactive web sites and many use it for day-to-day programming tasks. There are several versions in general use and sometimes the configuration of PHP between servers and machines needs to be compared. The phpinfo() function is a one line solution to show the current live PHP set up. To show PHP settings simply create a one line PHP web file on the server containing <?php phpinfo(); ?> and point the browser at it. HTML tags are NOT required because the phpinfo() function pumps them out.

PHP LogoNote: phpinfo() outputs a lot of useful information, information that hackers find interesting so use it with care. Ideally do not have the phpinfo() page on a public facing web site. On the occasions you do take precautions to reduce information leakage. Put the page in a password protected directory, do not call it phpinfo.php as this is obvious to hackers (use something more obscure and a reminder to delete it when finished, e.g. quick-config-check.php), finally don’t forget to delete it when the PHP settings have been checked.

A PHP script will need access to other settings that PHP provides, often via system wide globals known as the superglobals. The $_SERVER array provides access to the _SERVER superglobal and is shown by phpinfo() in a table. Occasionally it can be worthwhile viewing such values from another PHP file. This can be done in a few lines of code. The following provides some details on showing PHP settings and global values in web pages.

Show PHP Settings Examples

Normally a minimal web page would require the following tags for a basic structure:

But to show PHP settings only one line is required. Create a PHP file, e.g. php-test.php, on the web server’s public directory (on an Apache server this default may be /var/www/html). Add the call to phpinfo() to the file and save it:

Point the browser at the web page, e.g. http://www.example.com/php-test.php:

Show PHP Settings with phpinfo()

This produces a lengthy web page with comprehensive information on the PHP and web server configuration. The PHP variables stored in the $_SERVER global array are towards the end of the page. To see just the variables change the call to phpinfo() to phpinfo(INFO_VARIABLES), for other options for phpinfo() see the phpinfo documentation.

Showing the $_SERVER Settings in a PHP Script

The _SERVER superglobal is an array of useful strings, e.g. REQUEST_URI. Each value or the whole array can be output from any web file to aid debugging, not just through phpinfo(), here is an example a PHP file to print the whole array (to read a single value simple use the key required, e.g. $request_url=$_SERVER[“REQUEST_URI”];):

Notice the use of the => operator to assign the name of the array key to the $key variable, see the PHP documentation on foreach.

php_server_variables

See the PHP superglobals documentation for further information.

An alternative method is capture the output of phpinfo() for display in your own PHP files:

Storing and Displaying PHP Application Settings

An array can be used to support configuration information for your PHP application itself. Add the required settings to a key, value array and use the same method as for the superglobals to read the values:

PHP Application Settings

Displaying PHP Magic Constants

There are eight constants available that relate to the particular PHP script being executed, see the PHP Magic constants documentation for information on all eight. The __DIR__ and __FILE__ constants are probably the most used of the eight:

php_magic_constants

A Comprehensive Web Page to Show PHP Settings

Finally combining the information discussed here it is possible to produce a PHP page that displays a lot of useful environment and application information:

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.