Why wont my browser recognise php scripts within a html file? - php

I am designing a website that requires me to show the outputs of a mysql database on a webpage written in html. However my browser doesn't seem to recognize the php scripts within the html file and returns blank values where their should be details. The scripts worked fine in the .php files but have no effect when included in the .html files even after creating a .htaccess file as instructed in previously asked questions. Is there anything else that can be done to solve this issue?

There are two methods (that I've encountered) of changing the environment on an Apache server via .htaccess to allow PHP scripts to be processed in .html files, depending on the configuation. Whichever of these you've attempted, try the other.
AddHandler application/x-httpd-php5 .php .html .htm
Or
AddHandler cgi-script .html
SetEnv PHP_EXTENSION .html

Related

Using .htaccess to run PHP code within index.html [duplicate]

This question already has answers here:
Parsing HTML files as PHP
(3 answers)
Closed 5 years ago.
I'm trying to test executing php code within my index.html. To test this I'm using:
<?php
phpinfo();
?>
If I change the file to .php it works fine, but I want it to execute as .html . I set up a .htaccess to do this but it's not working. This is my .htaccess:
<Files index.html>
AddType application/x-httpd-php5 .html
</Files>
Both index.html and .htaccess are located in var/www/html. Why is the .html file not processing as .php when it has php code?
The reason why the web server is not passing the index.html file thru the PHP interpreter is that the mime-type has not been associated with it.
You need to use the AddHandler directive to set it.
In order to associate the type you've added, it really depends on what your setup is. Various scenarios might be: php-fpm, mod_php, cgi.
An example using CGI is:
Action application/x-httpd-php5 "/path/to/php"
Bear in mind that this setting is usually not available on shared hosting. If you are using one, consider contacting the support helpdesk.
Consider having a look at this duplicate question: Parsing HTML files as PHP
I would suggest against adding a handler. You should rather rename the index.html file to index.php. If this is not picked by Apache, you can correct it using DirectoryIndex index.php.
I hope this helps.

htacces file & PHP parsing issue

There is a problem with PHP parsing the html files on my site.
I've been using php in html files, together with the directive
AddHandler application/x-httpd-php5 .html .htm
in the .htaccess file, without any problem for 5 years.
Since a couple of days ago, suddenly it doesn't work anymore.
Only the .php files are parsed properly and the html files are not, anymore
(the actual php code is displayed instead of the result of the execution!)
I contacted the host service but they say they can't offer support.
I thought that for some reason the .htaccess file is not read anymore by the server,
but I tested it with a redirect directive and it worked.
Then I thought that they modified something in the php engine and beside the above mentioned directive I've tried each of the following directives:
AddHandler application/x-httpd-php .html .htm
AddType application/x-httpd-php5 .html .htm
AddType application/x-httpd-php .html .htm
AddHandler x-mapp-php5 .html .htm
but no success.
So html file are not php parsed anymore, but I've noticed someting interesting about the index.html file:
if I access mydomain.com/index.html the result is bad,
HOWEVER, if I access just mydomain.com then it displays properly !!
(And of course, there is no index.php file). That means the index.html file is actually parsed, in this case.
BUT, the situation stays the same even if the .htaccess file is deleted.
For this particular file (index.html), the behavior is the same in any of the subdirectories.
As the hosting service is not helpful with checking their server configuration settings,
I wonder if there is anything I can try on my side, as I'm stuck with this.
And any hints would be welcome.
Thanks.

After setting .htm files to render using PHP I'm being asked to download the page

I'm working on an old site, and I want to add cookie functionality in order to allow people to use the mobile site, but also opt-into using the full site if they want. The site is currently all .htm files, and I can't with the budget go in and change every link to .php so I tried adding this to my .htaccess file
AddType application/x-httpd-php .htm
This is causing the browser I'm using (Firefox) to ask me to download the file. It says it's a application/x-httpd-php file, so I know the .htaccess file is working. When I was building my home web sever, and trying to run a ruby on rails site I ran into the same problem because i hadn't set up ruby correctly and it wasn't rendering the file. But I have never run into a site that doesn't have some sort of support for PHP. Could this be caused by another problem. Or does that .htaccess file change break rules made by some web hosts?
Any support would be amazing! Thank you so much everyone :)
You need to add a handler for that type, otherwise the webserver isn't going to do anything with it:
AddHandler application/x-httpd-php .htm
That should be enough if you've already got phpv4 executing on your server. But you can also create a custom action explicitly:
AddHandler application/php-cgi .htm
Action application/php-cgi /path/to/php-handler

Adding Handler to htaccess causes file to download, removing Add Handler directive causes internal server error

I'm stuck trying to figure out what directives my htaccess file for a directory needs. To give a little background, I was messing around with AddType and AddHandler to try to get html files to run php in them. I was adding directives like:
AddType application/x-httpd-php .php .html
and
AddType application/x-httpd-php5 .php .html
That didn't work, so I tried adding handlers as well:
AddHandler x-httpd-php .php .html
and
AddHandler application/x-httpd-php .php .html
That still didn't give me what I wanted so I tried many combinations, like:
AddType application/x-httpd-php .php .html
AddHandler php-script .php .html
I even tried combinations with SetHandler
AddType application/x-httpd-php .php .html
AddHandler php-script .php .html
SetHandler application/x-httpd-php .php html
I tried every imaginable combination of AddType and AddHandler found on the web.
!===== This is important ======!
I realized that its not good practice to have php run your html files just so you can run php in the html. I am no longer trying to do that. The only reason I'm posting this part above is to give you an idea on some things I've changed. These changes were made to the htaccess file at a directory above the root.
!==============================!
Now that I'm no longer trying to run my php in html files I cleared the AddType, AddHandler, and SetHandler directives from the htaccess file. But now I get an internal server error when trying to run php files.
So I tried putting some stuff back. If I had a handle, the browser now downloads the php instead of running it.
I should note that any php running below the directory I changed works fine. But everything in that directory or subdirectory of that directory has this same problem with php files.
I have tried deleting the htaccess file in that directory, but that does not work, I get an internal error.
If I turn Override off, I get an internal error.
Something I changed is persisting in that directory and I'm wondering how to either
a. turn it off
b. add the correct handler to that htaccess file so that php files are run again (like they are on the rest of the server)
I did have the exact same issue so I deleted all the content of .htaccess and started trying only with handlers until I figured it out. My local .htaccess copy had the following SetHandler:
AddHandler x-httpd-php5-3 .php
In my case it worked after changing it to:
AddHandler application/x-httpd-php5 .php
It seems the whole trick is only the handler, I tried this solution in 2 different servers with different configurations and in both cases just changing AddHandler worked. If this doesn't work for you try a different handler for php.
Hope it helps.
Just realized I left this up unanswered after I solved the problem. If someone has a similar problem and is reading this : Check the permissions on all associated folders. I can't remember the specifics but one of the directories involved (I believe it was the htaccess directory) had permissions that persisted after changing some things and then changing them back.

what does .htaccess with line AddHandler php5-script .php do?

I am with new web host. The public_html folder of each domain I create is auto generated with an .htaccess that has the following line:
AddHandler php5-script .php
What is this for?
This just instructs PHP to handle files ending in .php by passing them to the PHP5 interpreter. Without this configuration in place, the web server may serve the files to the end-user's web browser as raw PHP code, rather than executing the code. That raises the dangerous possibility of exposing database login credentials or, or other secrets.
Using the same mechanism, you could configure the web server to parse files with other extensions besides .php as PHP scripts and hand them to the PHP interpreter. This is occasionally done to mask PHP scripts by naming them with .html extensions, for example.
# Interpret both .php & .html as PHP:
AddHandler php5-script .php .html
It tells php to handle any file with .php in the filename, even if it's not at the end. A file named smile.php.gif will be interpereted as a php file, which is bad if you are going to be using an upload script. This is because Apache allows multiple extensions in any order, so gif.php.jpg is the same as gif.jpg.php. I have heard the best way to select the handler is with FilesMatch. Of course if your web host has this in their httpd.conf you would have to 'remove' it using your htaccess before using the FilesMatch if you don't have access to httpd.conf.
The answer is that the htaccess tells the webserver to handle the php as php5-script and execute it.
Regarding the first answer, you will achieve your goal but it is a really bad practice and you should not allow html files to be executed as php due to huge security concerns.

Categories