If I have a file, file.php, in the same directory as index.html:
file.php:
<?php
echo "<body><img src=\"hi.jpg\" /><br /><p>I was here</p>"
?>
index.html:
<html>
<head></head>
<!-- I want to enter the PHP in file.php here --></body>
</html>
How do I put file.php in index.html?
Rename index.html to index.php, and fill it with the following:
<html>
<head></head>
<?php
require('./file.php');
?>
</html>
This worked for me:
Add the following to your .htaccess file
AddType application/x-httpd-php .php .htm .html
AddHandler application/x-httpd-php5 .html .php .htm
Related
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
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.
This question already has answers here:
How do I add PHP code/file to HTML(.html) files?
(12 answers)
Closed 8 years ago.
I am trying to run a simple php code inside my html file. The file name is test.html.
The following code is inside the file:
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
I have also changed my .htaccess file to include a code which my service provider recommended which is: AddHandler x-httpd-php .html.
But I still cannot display a simple message. Everything works fine if I change the file name to .php. But I want to keep my file name .html.
I can provide more info if required.
Try
AddType application/x-httpd-php .html .htm
or
AddType application/x-httpd-php5 .html .htm
or
RemoveHandler .html .htm
AddType application/x-httpd-php .html .htm
or
RemoveHandler .html .htm
AddType application/x-httpd-php5 .html .htm
Instead of
AddHandler application/x-httpd-php .html .htm
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
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/