Unable to serve HTML files containing PHP code

belltown

New member
Once upon a time, I used to be able to include PHP code in my HTML files. However, this no longer seems to be working for me.

Let's say I have this file, phpinfo.htm

Code:
<?php
echo phpinfo ();
?>

I have this line in my .htaccess file:

Code:
AddHandler application/x-httpd-php5 .htm

When I request the file from a browser (Firefox or IE), I get back the correct Content-Type header (text/html); however, instead of executing the PHP code, the server is just returning the contents of the file.

Is there anything else I need to do to get this to work?
 

un4saken

Administrator
Can you try this?

Code:
AddHandler application/x-httpd-php5 .html .htm .php

If that doesnt work, try to change your "PHP version" to a different version in your cPanel
 

GigaGreg

Moderator
Staff member
Why would you include a php code in the html, when you can just make index.php it will be the same as html version?

My index.php works very well, because it is all html and where I need, I just add a php code:

PHP:
  <?php require 'testimonials.php'; ?>
[/code]

As an example, which will require a data from another php file. This is so much easier and not complicated.
 

belltown

New member
un4saken said:
Can you try this?

Code:
AddHandler application/x-httpd-php5 .html .htm .php

If that doesnt work, try to change your "PHP version" to a different version in your cPanel

If I use: AddHandler application/x-httpd-php5 .html .htm .php then I get the same result with html and htm files, but php files are served up with the wrong content type: application/x-httpd-php. If I add: AddType text/html .html .htm .php, I get the correct Content-Type, but the php code is not executed either in my html or php files.

un4saken said:
If that doesnt work, try to change your "PHP version" to a different version in your cPanel

I've tried changing the PHP version from 5.5 to 5.4 in cPanel. It still doesn't work.

ogah said:
dont echo phpinfo()
PHP:
<?php
phpinfo();
?>

The problem has nothing to do with the phpinfo() call. I get the same result if I do this:

Code:
<?php
echo "<html>";
echo "<head>";
echo "</head>";
echo "<body>";
echo "<p>Hello</p>";
echo "</body>";
echo "</html>";
?>
The output looks like:
Code:
"; echo ""; echo ""; echo ""; echo "
Hello
"; echo ""; echo ""; ?>

iGdesigner said:
Why would you include a php code in the html, when you can just make index.php it will be the same as html version?

Because some html files that have a lot of html code with just a few lines of php are easier to edit if they have the ".html" extension. Editors treat .html and .php files differently, different syntax highlighting, for example. It's therefore more convenient for a predominantly html file to have a ".html" extension. For now I can edit them as .html, then change the extension to php when deploying then, although I'd rather not have to do that.

That still doesn't explain why something that used to work, and should work, no longer works.
 

fouadChk

Member
belltown said:
Once upon a time, I used to be able to include PHP code in my HTML files. However, this no longer seems to be working for me.

Let's say I have this file, phpinfo.htm

Code:
<?php
echo phpinfo ();
?>

I have this line in my .htaccess file:

Code:
AddHandler application/x-httpd-php5 .htm

When I request the file from a browser (Firefox or IE), I get back the correct Content-Type header (text/html); however, instead of executing the PHP code, the server is just returning the contents of the file.

Is there anything else I need to do to get this to work?

No there is nothing you can do about it for 2 reasons:
> 1- you're trying the wrong handler (ie application/x-httpd-php5.) This one is needed when Apache is handling PHP via mod_php.
Gigarank's server is loading PHP via mod_fcgid that's why your code doesn't work.

> 2- Thus the code should be:
Code:
<IfModule fcgid_module>
AddHandler fcgid-script .htm .html
FcgidWrapper /opt/alt/php56/bin/php-cgi .htm virtual
FcgidWrapper /opt/alt/php56/bin/php-cgi .html virtual
</IfModule>

Of course the path to php-cgi binary is only known by the server admin. The example above is using the path inferred from the output of phpinfo but it doesn't work (500_________errors.)
 

GigaGreg

Moderator
Staff member
Closed.

Feel free to open a new topic if you have any questions, issues, suggestions or feedback.
 

Genesis

Administrator
Staff member
Thread reopened at request of fouadChk who would like to contribute a solution to the problem.
 

fouadChk

Member
Genesis said:
Thread reopened at request of fouadChk who would like to contribute a solution to the problem.
Thanks Genesis!..

My last post here did give half of the answer when it was pointing out that the code used in the OP should take into account the PHP handler used by GR, but the other half of it, which is giving the actual working code, was falling short as I'm not too familiar with cPanel administration thus wasn't sure about the right path to the PHP handler. Today while I was browsing I did came across a hint about that path in cPanel own forums.

Thus, the working code to put in the .htaccess file is the following:
# For mod_fcgid
<IfModule fcgid_module>
AddHandler fcgid-script .htm .html
FcgidWrapper /usr/local/cpanel/cgi-sys/php5 .htm
FcgidWrapper /usr/local/cpanel/cgi-sys/php5 .html
</IfModule>

I did try it, and it's working flawlessly. Hope it helps.

Good Luck!