19

I have PHP installed on my CentOS server. However, when running a phpinfo() inside my script to test it, I receive the HTML, not the interpreted information.

I can see the folders for PHP. I can even see the php.ini in the etc folder. But PHP itself does not seem to be working.

I mean my test.php file looks like this:

<?php
    phpinfo();
?>

And the response looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html><head>
<style type="text/css">
body {background-color: #ffffff; color: #000000;}
body, td, th, h1, h2 {font-family: sans-serif;}
pre {margin: 0px; font-family: monospace;}
a:link {color: #000099; text-decoration: none; background-color: #ffffff;}
...

and so on.

What seems to be the problem and how do I solve it?

If I copy the HTML returned, paste it into an HTML file, and run it from there, I can see the formatted result, but not by running the test.php. I assume PHP is not loaded somehow... even if in the interpreted HTML I can see the:

**Server API    Apache 2.0 Handler
Virtual Directory Support    disabled
Configuration File (php.ini) Path    /etc/php.ini
Scan this dir for additional .ini files    /etc/php.d
additional .ini files parsed    /etc/php.d/dbase.ini, /etc/php.d/json.ini, /etc/php.d/mysql.ini, /etc/php.d/mysqli.ini, /etc/php.d/pdo.ini, /etc/php.d/pdo_mysql.ini, /etc/php.d/pdo_sqlite.ini
PHP API    20041225
PHP Extension    20050922
Zend Extension    220051025
Debug Build    no
Thread Safety    disabled
Zend Memory Manager    enabled
IPv6 Support    enabled
Registered PHP Streams    php, file, http, ftp, compress.bzip2, compress.zlib, https, ftps**

and so on...

On this system, there are three websites hosted. Does that have anything to do with this problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1137313
  • 2,390
  • 9
  • 44
  • 91
  • It sounds like PHP is running, and `phpinfo()` is outputting the right result. You just might have some templates or something getting in the way and throwing off the result. – cHao Jul 22 '13 at 00:14
  • 1
    That HTML code you're seeing is the same thing i see when i "view source" on a page that has nothing but `` in it. It's the HTML header of the PHP info page. – cHao Jul 22 '13 at 00:18
  • Yes. It's the HTML code that is returned normally on a normal phpinfo return page. But it is not interpreted. I see it without clicking "view source". What should I do? – user1137313 Jul 22 '13 at 00:33
  • If I remember correctly, php outputs HTML when doing a phpinfo() from inside a script running on a web server. Try this from the command line: php -i or run your script from the command line. – mlewis54 Jul 22 '13 at 00:33
  • I did run php -i from the commandline, and I received a lot of text. Seems like what phpini() should return. However, since it's not working from the browser, I tend to believe there is something wrong somewhere. As I stated before, I know I have php installed, but it does not seem to be working properly. – user1137313 Jul 22 '13 at 00:49
  • Is there any code that runs before yours? Like maybe a front controller / router, or something set in `auto_prepend_file`? – cHao Jul 25 '13 at 13:30
  • Had a similar issue, (except when I opened the html page it would comment out the php code). Changing the file name from index.html to index.php fixed it. Using apache2 and php7.0 – kiwicomb123 Dec 21 '17 at 11:19

14 Answers14

23

This happened to me as well. The fix was wrapping it in HTML tags. Then I saved the file as /var/www/html/info.php and ran http://localhost/info.php in the browser. That's it.

<html>
    <body>
        <?php
            phpinfo();
        ?>
    </body>
</html>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user6204369
  • 231
  • 2
  • 2
  • Using phpinfo(); won't display info at all. To print the configuration info's you need to echo it. So the code will be – Anbuselvan Rocky Oct 21 '17 at 11:06
  • I hit this after upgrading to OSX High Sierra [and php 7]. – Bret Weinraub Mar 27 '18 at 13:56
  • 1
    @AnbuselvanRocky is wrong. phpinfo() returns a bool. it outputs the info. if you want to capture its info use output buffering. phpinfo returns false on failure and true on success. – pgee70 Aug 24 '18 at 00:43
13

Save the page which contains

<?php
    phpinfo();
?>

with the .php extension. Something like info.php, not info.html...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Friend
  • 1,326
  • 11
  • 38
  • 62
10

You need to update your Apache configuration to make sure it's outputting php as the type text/HTML.

The below code should work, but some configurations are different.

AddHandler php5-script .php
AddType text/html .php
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cmorrissey
  • 8,493
  • 2
  • 23
  • 27
8

This did it for me (the second answer): Why are my PHP files showing as plain text?

Simply adding this, nothing else worked.

apt-get install libapache2-mod-php5
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sam Bing
  • 2,794
  • 1
  • 19
  • 29
4

I just had the same issue and found that a client had disabled this function from their php.ini file:

; This directive allows you to disable certain functions for security reasons.
; It receives a comma-delimited list of function names. This directive is
; *NOT* affected by whether Safe Mode is turned On or Off.
disable_functions = phpinfo;

More information: Enabling and disabling phpinfo() for security reasons

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Magnolia
  • 16,769
  • 36
  • 159
  • 270
3

Be sure that the tag "php" is stick in the code like this:

?php phpinfo(); ?>

Not like this:

? php phpinfo(); ?>

OR the server will treat it as a (normal word), so the server will not understand the language you are writing to deal with it so it will be blank.

I know it's a silly error ...but it happened ^_^

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

Try to create a php.ini file in root and write the following command in and save it.

disable_functions =

Using this code will enable the phpinfo() function for you if it is disabled by the global PHP configuration.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ahmad
  • 507
  • 1
  • 11
  • 22
2

It happened to me as well. On a newly provisioned Red Hat Linux 7 server.

When I run a PHP page, i.e. info.php, I could see plain text PHP scripts instead of executing them.

I just installed PHP:

[root@localhost ~]# yum install php

And then restarted Apache HTTP Server:

[root@localhost ~]# systemctl restart httpd
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Omari Victor Omosa
  • 2,814
  • 2
  • 24
  • 46
2

For people who have no experience in building websites (like me) I tried a lot, only to find out that I hadn't used the .php extension, but the .html extension.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pmkruyen
  • 142
  • 13
1

My solution was uninstalling and installing the PHP instance (7.0 in my case) again:

sudo apt-get purge php7.0
sudo apt-get install php7.0

After that you will need to restart the Apache service:

sudo service apache2 restart

Finally, verify again in your browser with your localhost or IP address.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

I had the same problem.

The solution in my case was to set default_mimetype = "text/html" inside the php.ini file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
W.M.
  • 589
  • 1
  • 6
  • 13
0

It may not work for you if you use localhost/info.php.

You may be able to found the clue from the error. Find the port number in the error message. To me it was 80. I changed address as http://localhost:80/info.php, and then it worked to me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
M Kamakshaiah
  • 59
  • 1
  • 2
0

Another possible answer for Windows 10:

The command httpd -k restart does not work on my machine somehow.

Try to use the Windows 10 Service to restart the relative service.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
miehmg
  • 93
  • 1
  • 1
  • 7
0

I accidentally set the wrong file permissions. After chmod 644 phpinfo.php the info indeed showed up as expected.

kghbln
  • 858
  • 2
  • 8
  • 18