How to Use PHP Code in .htm or .html pages - php

I am writing some PHP code in my .htm and .html pages but it's not executing the code.
I must strictly not have to change the page extension.
I have added this handler to the .htaccess page
AddHandler application/x-httpd-php74 .php .htm
AddHandler application/x-httpd-php74 .php .html
My server PHP version is PHP 7.4
What I am missing, how to use PHP code in .htm and .htm pages?
I am doing this on siteguard hosting so if any server configuration I may missing?

Sometimes the .htaccess file doesn't work on my machine. Try putting it in the virtual host configuration file (/etc/apache2/sites-enabled/[file name].conf)
RewriteEngine On
AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm
Tested on Debian 11 64-bit PHP 7.4, Apache 2.4.53
I don't know if your hosting will allow that modifications. It probably will work in the .htaccess file.
Hopefully it helps!
Credit to Moshe Gross and Constantin

Here Moshe Gross's method works!
this is only for test purpose:
i am tryin in xampp:
.htaccess
RewriteEngine On
AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="Custa">
<title></title>
</head>
<body>
<style>p{color:red;font-size:20px;}</style>
<p>yep yep</p>
<?php
echo 'wasn`t so difficult! '.time();
?>
</body>
</html>
when i access http://localhost/testhtmlphp/ i got this result:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="Custa">
<title></title>
</head>
<body>
<style>p{color:red;font-size:20px;}</style>
<p>yep yep</p>
wasn`t so difficult! 1656790012</body>
</html>
re: if i press F5 (refresh page) the timer change!

Add the following to your .htaccess
AddType application/x-httpd-php .html

PHP able to work with CLI or .php script files otherwise doesn't work. Because before from run PHP need compile.
You can use .htaccess abilities instead write code in HTML files. Just like the following code sample.
RewriteRule ^view\.php?mode=prod&id=([0-9]+) /products/$1.html
Creating dynamic URLs in htaccess

Related

php and SSI conflict on apache server

I currently have an html page that uses SSI. I now need to add some php code to that html page:
<?php
session_start();
$_SESSION["animal"] = "dog";
?>
<!DOCTYPE html>
<html>
I can successfully implement either the SSI or the php, but not both. I believe the problem is here, in my httpd.conf file:
AddType text/html .html
AddOutputFilter INCLUDES .html
AddHandler application/x-httpd-php .php
AddType application/x-httpd-php .php .html
LoadModule php5_module "C:/php5/php5apache2_4.dll"
PHPIniDir "c:/php5"
</IfModule>
I have 2 different lines for AddType, and which ever AddType is listed second, is the one that works. Is there a way to get both AddType's working at the same time?
Thanks!
Use FilesMatch and SetHandler and put this in .htaccess instead of httpd.conf:
# Enable server side includes
Options +Includes
# Handle files ending in .php, .shtml, and .html using the PHP interpreter:
<FilesMatch "\.(php|shtml|html)$">
SetHandler application/x-httpd-php
</FilesMatch>
# Filter .php, .shtml and .html files through mod_include first
AddOutputFilter INCLUDES .php .shtml .html
And test both in the same page:
<!DOCTYPE html>
<head>
<title>PHP/SSI Test Page</title>
</head>
<body>
<!--#echo var="DATE_LOCAL" -->
<? echo "Last Updated: ".date("F jS Y",
getlastmod() ); ?>
</body>
</html>
References
mod_mime: AddOutputFilter Directive
<FilesMatch> Directive
Notes on Server Side Includes SSI Syntax KB 203064 Revisited
Web Hosting Glossary
httpd.conf-dist
Apache::SSIChain

How to get a PHP file to run with an ".html" extension?

I have to execute PHP codes in a HTML file, but my server not doing it. I added a handler AddHandler application/x-httpd-php .html .htm but still it's not responding to the code.
The code I tried to execute
<!DOCTYPE html>
<html>
<body>
<?php
$url=basename($_SERVER['REQUEST_URI']);
?>
<p>the file name is <?php echo $url; ?></p>
</body> </html>
and the result is "the file name is"
it should be "the file name is test.html"
I checked the server software version via phpinfo() it say this "Apache Phusion_Passenger/4.0.10 mod_bwlimited/1.4 mod_fcgid/2.3.9". now what ?
I don't even know that it is. Please tell me what server my web host is using and how can I run PHP codes in a HTML file?
You can try one of the following (probably, add to your .htaccess):
RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html
Or
<FilesMatch ".+\.html$">
SetHandler application/x-httpd-php
</FilesMatch>
Please rename the file from test.html to test.php and browse the file.It will work.

PHP block is not parsed in html

I am writing the following html code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
function detectBrowser(){
if(navigator.appName === "Microsoft Internet Explorer"){
window.open("redirect.html", "_parent");
}
}
</script>
</head>
<body onload="detectBrowser()">
<div class="mainBody">
<?php
echo "test";
?>
</div>
</body>
</html>
But the php block doesn't display test for me. It looks like it is not parsed and I can see the php code in the webpage's source. Any one can tell the problem? Thank you
You can enable parsing of PHP in files with .html extensions in your httpd.conf file.
Look for a line like this and make sure .html is an option.
AddType application/x-httpd-php .php .php3 .phtml .html
You could probably add it to an .htaccess file in the docroot of you site as well, but I've not tried this personally.
You could change this in your .htaccess file. (if it doesn't exist just create the file (text only) in the same folder as your file)
AddType application/x-httpd-php .html
However this would make all static files also go through the PHP parser which is unnecessary.
I would suggest the best solution is to rename the file instead with extension .php. Then in your .htaccess file you could rewrite just this address, then you will be able to access it with the .html extension anyway.
RewriteEngine on
RewriteRule ^myfile.html$ myfile.php

PHP code is just comment on my html

For some reason my code is just comment in my html.
I have tried using localhost/filename.html etc
and nothing seems to work...
Thank you!
<html>
<body>
<?php
echo "hello world";
?>
</body>
</html>
If you are running apache you could add the following in your .htaccess file to process .html files as they were php:
RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html
Here are a couple of links further explaining the useage of .htaccess files:
http://httpd.apache.org/docs/1.3/howto/htaccess.html
http://www.htaccess-guide.com/

PHP script not working in HTML file

I'm new to PHP. I installed XAMPP and have Apache running. I created helloworld.php in XAMPP's htdocs and got PHP to display in my browser. My question is, why does my PHP script in my HTML file not display in my browser? Ive never installed PHP on its own. Should I also install it? Would it conflict with XAMPP. My code is below. Any assistance will be appreciated. Thanks in advance:
<html>
<body>
<?php
echo "Hello PHP World";
?>
</body>
</html>
I assume you are trying to use php inside .html file?
Try adding .htaccess file or changing apache config with the following line:
AddHandler application/x-httpd-php .html
XAMPP already includes PHP, but unless you end the script name with .php it is unlikely to be processed by the PHP engine.
Stop the apache service, then add one change in c:\xampp\apache\conf\httpd.conf in the section by adding...
AddType application/x-httpd-php .html .htm
Restart apache!
This looks like a big fat 'feature' in the current xampp distribution for win 32-bit.
You should add mime type at http conf
for instance in apache at httpd.conf
entry
<IfModule mime_module>
#
# TypesConfig points to the file containing the list of mappings from
# filename extension to MIME-type.
#
TypesConfig "conf/mime.types"
.......
AddType application/x-httpd-php .html .htm
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
The php module for apache registers itself as handler for the mime type application/x-httpd-php. And the configuration file apache\conf\extra\httpd-xampp.conf contains the lines
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
which tells the apache that all files having .php as name extension are to be processes by the handler for application/x-httpd-php.
If you (really) want to have your .html files handled by the php module as well you have to add something similar for .html extensions. (there are other methods to tell the apache which extension maps to which mime type/handler. But FilesMatch/SetHandler is fine.)
If you want to enable this "feature" for only one directory you can use an .htaccess file to change the configuration for that directory (and its subdirectories).
Too much overkill. All these suggestions lead me down the wrong path for like 5 hours. JK, but I did read a lot of google search items all giving wrong answers and each suggestion was just adding more wrong answers.
The answer is in fact so simple you would want to bang your head: Simply change the file extension from ".html" to ".php"!!! Remember that you can build a webpage entirely out of PHP and all JavaScript and stuff built off JavaScript like, JQuery, bootstrap, etc will work.
Here is a simple example of proof:
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Blank Web Page</title>
<link rel="stylesheet" type="text/css" href="css/css.css">
</head>
<body>
<?php
$son = 5;
$nos =10;
echo $son + $nos;
?>
<h4>test to see if this html element can be output too!</h4>
<script type="text/javascript" src="js/js.js"></script>
</body>
Notice that I am using your standard html, even though it doesn't show my HTML tags(trust me it's there), web page stuff and have php code inserted inside. Of course the result is 15 and the html element h4 renders correctly too. Change the extension back to "html" and you will get only the h4 element and you will find that your php code has been commented out using multi-comment for html.
I forgot to add that this works for Xampp too.

Categories