Tek Eye Logo

Tek Eye

Show PHP Settings with phpinfo and Example PHP $_SERVER Display 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.

PHP Logo

View PHP Environment Configuration Settings and Superglobals on a Page

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.

Note: phpinfo() outputs a lot of useful information, information that hackers find interesting, use it with care. Ideally do not have the phpinfo() page on a public facing web site. On the occasions you do expose it, 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 rest of this article provides some examples on showing PHP settings and global values in web pages.

(See Copying Code from the Articles if you need help on copying the code shown in this tutorial.)

Show PHP Settings Examples

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

<!DOCTYPE html>
<html>
    <head>
        <title>Basic Web Page</title>
    </head>
    <body>
Content goes here!
    </body>
</html>

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 (for the Apache server the default directory location for web files is usually /var/www/html). Add the call to phpinfo() to the file and save it:

<?php
    phpinfo();
?>

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, under PHP Variables. 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"];):

<!DOCTYPE html>
<html>
    <head>
        <title>$_SERVER</title>
    </head>
    <body>
        <?php
            print "<table border=1>";
            foreach ($_SERVER as $key=>$val ){
                print "<tr><td>".$key."</td><td>".$val."</tr>";
            }
            print "</table>";
        ?>
    </body>
</html>

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:

<!DOCTYPE html>
<html>
    <head>
        <title>PHP Settings</title>
    </head>
    <body>
        <?php
            ob_start();
            phpinfo(INFO_VARIABLES);
            echo ob_get_clean();
        ?>
    </body>
</html>

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
    $APP_SETTINGS = array(
        "SQLITE_LOCATION" => "data/customers",
        "SQLITE_NAME" => "customer",
    );
?>
<!DOCTYPE html>
<html>
    <head>
        <title>App Settings</title>
    </head>
    <body>
        <?php
            print "<table border=1><caption>Our Settings (Array)</caption>";
            foreach ($APP_SETTINGS as $key=>$val ){
                print "<tr><td>".$key."</td><td>".$val."</tr>";
            }
            print "</table>";
        ?>
    </body>
</html>

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:

<!DOCTYPE html>
<html>
    <head>
        <title>Script</title>
    </head>
    <body>
        <?php
            echo "<p>".__DIR__."</p>";
            echo "<p>".__FILE__."</p>";
        ?>
    </body>
</html>

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:

<?php
    $APP_SETTINGS = array(
        "SQLITE_LOCATION" => "data/customers",
        "SQLITE_NAME" => "customer",
    );
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Script</title>
    </head>
    <body>
        <?php
            echo "<table border=1><caption>Our Settings (Array)</caption>";
            foreach ($APP_SETTINGS as $key=>$val ){
                echo "<tr><td>".$key."</td><td>".$val."</tr>";
            }
            echo "</table>";
            echo "<p>Script directory: ".__DIR__."</p>";
            echo "<p>PHP File: ".__FILE__."</p>";
            ob_start();
            phpinfo(INFO_VARIABLES);
            echo ob_get_clean();
        ?>
    </body>
</html>

Also See

  • View the Tek Eye full Index for other articles.

Author:  Published:  Updated:  

ShareSubmit to TwitterSubmit to FacebookSubmit to LinkedInSubmit to redditPrint Page

Do you have a question or comment about this article?

(Alternatively, use the email address at the bottom of the web page.)

 This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

markdown CMS Small Logo Icon ↓markdown↓ CMS is fast and simple. Build websites quickly and publish easily. For beginner to expert.


Articles on:

Android Programming and Android Practice Projects, HTML, VPS, Computing, IT, Computer History, ↓markdown↓ CMS, C# Programming, Using Windows for Programming


Free Android Projects and Samples:

Android Examples, Android List Examples, Android UI Examples



Tek Eye Published Projects