Writing phpinfo() To File

The contents of phpinfo() are quite useful, and it is usually the first thing that many developers perform to make sure that PHP is installed. However, printing out the phpinfo() function can lead to a security risk because it displays a lot of information about the server.

Here is how to write the contents of the phpinfo() function to a file.

ob_start();
phpinfo();
$info = ob_get_contents();
ob_end_clean();
 
$fp = fopen("phpinfo.html", "w+");
fwrite($fp, $info);
fclose($fp);

This is an example of output buffering.

Comments

Thanks for the tip. It would be good to just have it save as Plain Text format rather than html as this would make diff/merge of different server settings much easier. Is there a way to strip the html ?
Permalink
Well, you can generate it from the command line with php --infoThat doesn't have any HTML in it :)
Name
Philip Norton
Permalink

I know this is really old... but for us old timers that are just beginning this php journey, how/where do you run a script like this?

Thanks.

GOJ

 

Permalink

Hi GoodOlJoe, thanks for commenting.

You can run this script in two ways. 

1) On the command line.

Assuming you have PHP installed, create a file called something like phpinfo.php. Then, open up a terminal prompt, change directory to the same directory that the file is located, and run it like this.

php phpinfo.php

You will find that once it's run you'll have another file in the same directory called phpinfo.html.

2) Via a web server

This is slightly more tricky if you're new to PHP, but there are plenty of tutorials out there on installing PHP and a web server (Apache and Nginx are the main two).

Once you have it installed you need to put the script into the web directory in the form of a file called something like phpinfo.php. All you need to do then is visit the file through your web browser. This might have the following address

http://localhost/phpinfo.php

Both of these do depend on how you set things up though. In the world of docker containers there are numbers ways in which you can stand up a local web server and run both of these actions.

Good luck!

Name
Philip Norton
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
3 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.