What is the phpinfo file and how to create it

Author: HOSTTEST Editorial   | 20 Sept 2021

Create php.infoThe combination of Linux, Apache, MySQL database, and PHP interpreter - also known as LAMP - has been the standard for web hosting a website for about two decades. However, due to the complexity of the system, issues or errors often arise during setup, requiring a detailed diagnosis. One of the key tools that help narrow down the causes is the embedded function phpinfo in the PHP interpreter. Its use brings numerous practical advantages, establishing it as the primary tool to check a web server's properties and compatibility with specific requirements.

Contents:
What is the purpose of the phpinfo function?
What does the phpinfo function display?
How does phpinfo function as a tool?
How can phpinfo be used as a utility?
What issues can arise when using phpinfo?

What is the purpose of the phpinfo function?

Phpinfo is a method firmly integrated into PHP that retrieves all relevant data of the interpreter and its environment from various sources, summarising them clearly on a single webpage. This is specifically done through the simple PHP command phpinfo(). As evident from the two closing parentheses, this is a function that can be called without additional parameters and placed anywhere in a source code. The practical use is initially minimal on an operational server in production - unless an administrator wanted to provide visitors with details about the server configuration, which is not recommended from a security perspective.

The main task of phpinfo, apart from some applications like automatically analysing the environment, is troubleshooting and optimising web hosting and specifically the PHP environment. To enable a comprehensive evaluation, the function accesses various programs and tools of the operating system and collects all relevant data about an internet server. From this data, the function then generates a tabularly organised webpage where an administrator can find all essential information without having to extensively access and inspect the individual components database and web server as well as the PHP installation including the various configuration files.

Find PHP Web Hosting

Secure the perfect PHP web hosting now

Go to PHP Web Hosting Comparison

What does the phpinfo function display?

Although the phpinfo function was originally implemented for setting up and checking PHP itself, it provides the most important approach due to the variety of parameters it captures and evaluates, to also analyse the environment - that is, the combination of Apache or NGINX, available databases, and PHP. The display is clear in several tables, each assigned to a specific topic area. The output of phpinfo includes, in the order below, among other things:

  • Current version of PHP
  • General information about the server such as the kernel version and important directories
  • Global and local configuration of the web server
  • Detailed HTTP headers when a page is called
  • Support for data compression via Bzip2 and the calendar
  • Local and global configuration of PHP parameters
  • Details on various utilities like cURL, exif, or hash
  • Local and global configuration of MySQL parameters
  • Support and version of openssl for encryption
  • Integration of XML for data formatting
  • Information on Zend OPcache for performance optimisation
  • Key data about the environment such as the user used and directories
  • Variables used by PHP and their stored value
  • Credits, responsible developers, and the type of open-source license

All of this information can be relevant under certain conditions because PHP uses numerous external programs and system applications to fulfil its tasks as intended. These include, for example, Client for URLs (cURL) for data downloads and an FTP program to establish connections for uploading large files. Although this information is not generally confidential, it is advisable in principle not to make phpinfo permanently publicly accessible, as it can be misused as a tool for identifying vulnerabilities, such as through the use of a specific version of a program.

How does phpinfo work as a tool?

Phpinfo is a fixed-in function that any PHP script can call like a command at any point in its code. To use it, it simply needs to be inserted as the command phpinfo() into a new or existing script. In its simplest and most common form, the script looks like this:

(Less than sign)?php
phpinfo();
(Greater than sign)

The first and last lines instruct the interpreter that there is PHP code between the two arrows that should be executed line by line. In the middle is the phpinfo command with an empty bracket, as no additional parameters need to be passed for the task. The semicolon in PHP simply denotes the end of a line, indicating that the command ends at that point. Abstractly, it is therefore only a simple command that, when called, generates a dynamic webpage that retrieves data from various sources and then presents it in a clear and tabular form.

How can phpinfo be used as a tool?

A phpinfo script can be accessed by the user like a conventional webpage if it is located in a publicly accessible path within the content directory. To practically apply it, there are various options:

  • Create a new file via an SSH access
  • Insert the command into an existing PHP script
  • Locally create the file and upload it through an FTP program
  • Locally create the file and upload it through a web interface in the control panel

The most effective method is using SSH, as it provides direct access to the server and allows for the direct creation of the script. This method requires only a few steps:

  • 1. Connect to the server using an SSH client such as SSH (Linux) or PuTTY (Windows)
  • 2. Navigate to the public readable root directory of the site with cd path/to/website
  • 3. Start a simple text editor with nano phpinfo.php
  • 4. Copy the PHP code into the text field
  • 5. Close the editor with CTRL + X and confirm saving with j or y

Alternatively, existing scripts like the index.php file in the root directory can be opened with the nano editor to insert the phpinfo() command at the beginning of the code. However, this approach is not recommended due to security reasons as existing files should not be permanently altered, the display will occur within the existing page, and lastly complications with the remaining content of the required index.php script may arise.

The second option involves creating the PHP script locally on your workstation and then uploading it, for example, via an FTP program or a suitable web interface. It is important to note that Microsoft Windows and Linux use different control signals for the end of a line in a text file. Therefore, a compatible editor such as the free Notepad should be used for creation, and the format should be changed to Unix/Linux. Subsequently, the script just needs to be uploaded to a public directory using FTP or the interface.

The name of the script and the chosen directory are not important and can be freely chosen. For example, it is possible to hide the script as fix_abc.php in the Pictures subdirectory. The script can then be accessed by simply entering the used address in the format Domain.TLD/directory/name.php in any browser, for example, in the above-mentioned case:

https://Example.com/Pictures/fix_abc.php

The server will then recognise the script and pass it to the PHP interpreter.

What problems can occur when using phpinfo?

The only requirements for using phpinfo are a properly configured and running web server environment and the ability to access it directly like an external website. If a status code HTTP Error 404 Not Found or 403 Forbidden occurs, it means the file is not located where expected or lacks the necessary permissions for access. In the first case, the most common causes are that the directory is not within the website or the name is misspelled. It is important to note that Linux, unlike Microsoft Windows, is case-sensitive when it comes to file names. Therefore, the spelling of directories and names must exactly match the specified format. On the other hand, an error code 403 indicates that the file exists, but the web server does not have sufficient rights to read it. Full access can be easily granted using the SSH command chmod 777 name.php or setting permissions in an FTP program. If a server error occurs with a code like HTTP Error 500 Internal Server Error or 502 Bad Gateway, the cause lies in a significantly incorrect configuration of PHP or the web server. This must be rectified before using phpinfo or a website. For security reasons, it is strongly recommended to delete the function permanently after use, for example, using the SSH command rm name.php.

Find PHP Hosting

Secure the perfect PHP hosting now

Compare PHP Hosting

Photo: Lawrence Monk on Pixabay

Write a comment


More web hosts


More interesting articles

What you should know about PHP Extended Support

This article sheds light on the concept of PHP Extended Support, an initiative by hosting providers aiming to ensure thi...

Error 401 - how to fix the HTTP Error Unauthorized?

We show you how to easily fix the Error 401 unauthorized.