TVs. Consoles. Projectors and accessories. Technologies. Digital TV

Consideration info php. How to Create a Phpinfo File and Check PHP Information. Using the web server module

If you needed to send files not directly by the web server, but using PHP (for example, to collect download statistics), please refer to cat.

1. Use readfile()

The good thing about this method is that it works out of the box. You just need to write your own file sending function (slightly modified example from the official documentation):

Function file_force_download($file) ( if (file_exists($file)) ( // reset the PHP output buffer to avoid overflowing the memory allocated for the script // if this is not done, the file will be read into memory completely! if (ob_get_level()) ( ob_end_clean(); ) // force the browser to show the file saving window header("Content-Description: File Transfer"); header("Content-Type: application/octet-stream"); =" . basename($file)); header("Content-Transfer-Encoding: binary"); header("Expires: 0"); header("Cache-Control: must-revalidate"); header("Pragma: public"); header("Content-Length: " . filesize($file)); // read the file and send it to the user readfile($file); exit; ) )
Even large files can be sent this way, since PHP will read the file and immediately give it to the user in parts. The documentation clearly states that readfile() shouldn't create memory problems.

Peculiarities:

  • The file is read into the internal buffer of the readfile() function, the size of which is 8kB (thanks to 2fast4rabbit)

2. Read and send the file manually

The method uses the same Drupal when sending files from a private file system(files are not available directly via links):

Function file_force_download($file) ( if (file_exists($file)) ( // reset the PHP output buffer to avoid overflowing the memory allocated for the script // if this is not done, the file will be read into memory completely! if (ob_get_level()) ( ob_end_clean(); ) // force the browser to show the file saving window header("Content-Description: File Transfer"); header("Content-Type: application/octet-stream"); =" . basename($file)); header("Content-Transfer-Encoding: binary"); header("Expires: 0"); header("Cache-Control: must-revalidate"); header("Pragma: public"); header("Content-Length: " . filesize($file)); // read the file and send it to the user if ($fd = fopen($file, "rb")) ( while (!feof($ fd)) ( print fread($fd, 1024); ) fclose($fd); exit;
Peculiarities:

  • The script waits until the entire file is read and given to the user.
  • Allows you to save server memory

3. Use the web server module

3a. Apache
The XSendFile module allows you to send a file to Apache itself using a special header. There are versions for Unix and Windows, under versions 2.0.*, 2.2.* and 2.4.*

In the host settings you need to enable header interception using the directive:
XSendFile On
You can also specify whitelist directories in which files can be processed. Important: if you have a server on Windows based the path must include an uppercase drive letter.

Description of possible options on the developer's website: https://tn123.org/mod_xsendfile/

Example of sending a file:

Function file_force_download($file) ( if (file_exists($file)) ( header("X-SendFile: " . realpath($file)); header("Content-Type: application/octet-stream"); ​​header(" Content-Disposition: attachment; filename=" . basename($file)); exit; ) )

3b. Nginx
Nginx can send files out of the box via a special header.

For correct operation, you need to deny access to the folder directly through the configuration file:
location /protected/ ( internal; root /some/path; )
Example of sending a file (the file must be in the /some/path/protected directory):

Function file_force_download($file) ( if (file_exists($file)) ( header("X-Accel-Redirect: " . $file); header("Content-Type: application/octet-stream"); ​​header("Content -Disposition: attachment; filename=" . basename($file)); exit; ) )
More information on the official documentation page

Peculiarities:

  • The script ends immediately after all instructions are completed
  • Physically, the file is sent by the web server module itself, and not by PHP
  • Minimal consumption of memory and server resources
  • Maximum performance

Update: Habrowser ilyaplot gives good advice that it is better to send not application/octet-stream , but the real mime type of the file. For example, this will allow the browser to substitute necessary programs into the file save dialog.

This function displays a table containing many useful information about php settings, installed modules, configuration local server, environment variables, OS version, path information, HTTP headers, PHP license, etc.

The phpinfo function is used when checking the system, since each server on the network has its own unique settings. If the script worked fine on one server and stopped working on another, then it is worth checking the configuration settings of the new server using this function.

The easiest way to use this function is to create a separate file:

It will display a page like this:

Figure 1. The phpinfo() function works.

And there is about four more screens of information down there.

phpinfo is a function that prints full information about the current PHP configuration.

PHP phpinfo function
Name (constant)Meaning Description
INFO_GENERAL 1 General configuration information, php.ini location, information about the Web server, system, etc.
INFO_CREDITS 2 Information about PHP developers. It can also be obtained using the phpcredits() function.
INFO_CONFIGURATION 4 Information about the current values ​​of the main and local PHP directives They can also be obtained using the ini_get() function.
INFO_MODULES 8 Loaded modules and their settings. See also get_loaded_extensions() .
INFO_ENVIRONMENT 16 Information about environment variables, which is also available in $_ENV .
INFO_VARIABLES 32 Outputs all predefined variables from EGPCS (Environment, GET, POST, Cookie, Server).
INFO_LICENSE 64 PHP license information.
INFO_ALL -1 Outputs all of the above.

You can see that the phpinfo function contains EGPCS data, so it can be used for debugging.

The values ​​are bitwise, so the numbers used are 2, 4, 8, 16, 32, and 64.

Examples of using phpinfo() with arguments:

The information provided by the phpinfo() function can be used by attackers to harm your site. Therefore, files with this function must be deleted.

In 2010, the well-known Russian resource for programmers, Habrahabr, published an article that presented data from a very interesting study. It turned out that 4.69% of Russian sites contain a phpinfo.php file with the phpinfo function. At that time, the RuNet consisted of 36,804 sites, 1,725 ​​had potential vulnerabilities.

If you just need to find out what version of PHP is on the server, you can use the phpversion function.

Any software, which you want to run on your web server has certain requirements that it must meet. For example, WordPress requires PHP version 5.2.4 or higher. Depending on the server, you can change some PHP settings, others, on the contrary, are prohibited from changing, however, information about all of them can be found in the phpinfo file. In this tutorial, you will learn how to create a phpinfo file and find out the values ​​and status of PHP modules. This can also be useful for getting information about your hosting account such as max_execution_time, memory_limit, post_max_size and others.

Before you start this guide, you will need the following:

  • Access to your hosting control panel or FTP account

Option 1 - Checking PHP Information Through Your Hosting Control Panel

At Hostinger, your account's PHP information can be found in the Advanced → PHP Information. This is very convenient function, since you won't need to create additional files on your hosting.

After this, you will be taken to a page with all necessary information about your PHP version, modules and values. To search for a specific module or function, use the search by pressing the keyboard shortcut CTRL+F.

Congratulations! You have learned how to access your php information through the Hostinger control panel.

Option 2 - Checking PHP Information by Creating a phpinfo.php File

Don't worry if your hosting platform doesn't support the feature shown in Option 1. The same result can be achieved by creating a special file inside your hosting account. The file will also show all the information about your PHP and will be accessible through the browser.

Step 2.1 - Create a phpinfo file

There are several ways to create a phpinfo file. In this tutorial we will use . However, the same result can be achieved by creating a file on your local computer and further uploading the file to the server via .

Login to your hosting control panel and open File manager . Click the button New file to create a new file.

In the first field, indicate the path where it will be created new file. Next, in the field New file name enter phpinfo.php. In most cases you can leave the path unchanged /public_html. In this case, the file will be created in the root directory of your site.

Step 2.2 - Editing the file

At this stage you already have an empty file phpinfo.php in the catalog public_html. Copy the following code to a file and click the icon Save in the left corner of the screen.

That's it, you have successfully created a PHP file that will display all the parameters of your PHP. As mentioned earlier, the same result can be achieved using:

  1. Use any text editor and create a file phpinfo.php on your computer.
  2. Add the following code to the file:
  1. Upload the file using FTP to your directory public_html.

Step 2.3 - Checking PHP Information via Browser

If everything was done correctly, you can now access the created file by adding to the end of your domain name /phpinfo.php. For example, http://yourdomain.ru/phpinfo.php

You should see a similar result when opening this page through a browser:

On this page you will be able to see all your PHP settings.

Conclusion

By finishing this tutorial, you have learned how to create a phpinfo file and check your information. PHP settings. This information is useful if you want to know your hosting settings or run software that requires certain PHP modules.



Related publications