PHP code is just comment on my html - 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/

Related

Include PHP header in my HTML files [duplicate]

How can I run simple PHP code inside a .html file?
To execute 'php' code inside 'html' or 'htm',
for 'apache version 2.4.23'
Go to '/etc/apache2/mods-enabled'
edit '#mime.conf'
Go to end of file and
add the following line:
"AddType application/x-httpd-php .html .htm"
BEFORE tag '< /ifModules >'
verified and tested with 'apache 2.4.23'
and 'php 5.6.17-1'
under 'debian'
You can't run PHP in an html page ending with .html. Unless the page is actually PHP and the extension was changed with .htaccess from .php to .html
What you mean is:
index.html
<html>
...
<?php echo "Hello world";?> //This is impossible
index.php //The file extension can be changed using htaccess, ex: its type stays php but will be visible to visitors as index.html
<?php echo "Hello world";?>
thanks for the ideas but none works here. So i did that...
I am using xampp last version on 2014.
go to \xampp\apache\conf\extra\httpd-xampp.conf.
we will find this bit of code:
<IfModule php5_module>
**<FilesMatch "\.php$">**
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
PHPINIDir "C:/xampp/php"
</IfModule>
Focus on second line, so we must to change to:
<IfModule php5_module>
**<FilesMatch "\.(php|html)$">**
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
PHPINIDir "C:/xampp/php"
</IfModule>
And that is it. Works good!
Simply you cant !! but you have some possbile options :
1- Excute php page as external page.
2- write your html code inside the php page itself.
3- use iframe to include the php within the html page.
to be more specific , unless you wanna edit your htaccess file , you may then consider this:
http://php.about.com/od/advancedphp/p/html_php.htm
Yes, you can run PHP in an HTML page.
I have successfully executed PHP code in my HTML files for many years. (For the curious, this is because I have over 8,000 static HTML files created by me and others over the last 20 years and I didn't want to lose search engine ranking by changing them and, more importantly, I have too many other things to work on).
I am not an expert -- below is what I've tried and what works for me. Please don't ask me to explain it.
Everything below involves adding a line or two to your .htaccess file.
Here is what one host ( http://simolyhosting.net ) support did for me in 2008 -- but it no longer works for me now.
AddHandler application/x-httpd-php5 .html .htm
AddType application/x-httpd-php5 .htm .html
That solution appears to be deprecated now, though it might work for you.
Here's what's working for me now:
AddType application/x-httpd-lsphp .htm .html
(This page has PHP code that executes properly with the above solution -- http://mykindred.com/bumstead/steeplehistory.htm )
Below are other solutions I found -- they are NOT MINE:
https://forums.cpanel.net/threads/cant-execute-php-in-html-since-ea4-upgrade.569531
I'm seeing this across many servers I've recently upgraded to EA4. Using cPanel Apache handlers or adding this directly in to .htaccess (same as cPanel does through gui add handlers):
AddHandler application/x-httpd-php5 .html
Sep 9, 2016
AddHandler application/x-httpd-ea-php56 .html
https://help.1and1.com/hosting-c37630/scripts-and-programming-languages-c85099/php-c37728/parsing-php-code-within-html-pages-a602364.html
Open a text editor such as wordpad, notepad, nano, etc. and add the following line:
AddHandler x-mapp-php5 .html .htm
If you want to use PHP 5.4 instead of PHP 5.2 then use the following line instead:
AddHandler x-mapp-php6 .html .htm
https://www.godaddy.com/community/Developer-Cloud-Portal/Running-php-in-html-files/td-p/2776
To run HTML using FastCGI/PHP, try adding this code to the .htaccess file for the directory the script is in:
Options +ExecCGI
AddHandler fcgid-script .html
FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .html
You can add additional lines for other file extensions if needed.
You need to make the extension as .php to run a php code
BUT if you can't change the extension you could use Ajax to run the php externally and get the result
For eg:
<html>
<head>
<script src="js/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajax({
url:'php_File_with_php_code.php',
type:'GET',
data:"parameter=some_parameter",
success:function(data)
{
$("#thisdiv").html(data);
}
});
});
</script>
</head>
<body>
<div id="thisdiv"></div>
</body>
</html>
Here, the JQuery is loaded and as soon as the pages load, the ajax call a php file from where the data is taken, the data is then put in the div
Hope This Helps
I'm not sure if this is what you wanted, but this is a very hackish way to include php. What you do is you put the php you want to run in another file, and then you include that file in an image. For example:
RunFromHTML.php
<?php
$file = fopen("file.txt", "w");
//This will create a file called file.txt,
//provided that it has write access to your filesystem
fwrite($file, "Hello World!");
//This will write "Hello World!" into file.txt
fclose($file);
//Always remember to close your files!
?>
RunPhp.html
<html>
<!--head should be here, but isn't for demonstration's sake-->
<body>
<img style="display: none;" src="RunFromHTML.php">
<!--This will run RunFromHTML.php-->
</body>
</html>
Now, after visiting RunPhp.html, you should find a file called file.txt in the same directory that you created the above two files, and the file should contain "Hello World!" inside of it.
<?php
echo '<p>Hello World</p>'
?>
As simple as placing something along those lines within your HTML assuming your server is set-up to execute PHP in files with the HTML extension.

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 in .html file and .htaccess [duplicate]

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

How can I run a PHP script inside a HTML file?

How can I run simple PHP code inside a .html file?
To execute 'php' code inside 'html' or 'htm',
for 'apache version 2.4.23'
Go to '/etc/apache2/mods-enabled'
edit '#mime.conf'
Go to end of file and
add the following line:
"AddType application/x-httpd-php .html .htm"
BEFORE tag '< /ifModules >'
verified and tested with 'apache 2.4.23'
and 'php 5.6.17-1'
under 'debian'
You can't run PHP in an html page ending with .html. Unless the page is actually PHP and the extension was changed with .htaccess from .php to .html
What you mean is:
index.html
<html>
...
<?php echo "Hello world";?> //This is impossible
index.php //The file extension can be changed using htaccess, ex: its type stays php but will be visible to visitors as index.html
<?php echo "Hello world";?>
thanks for the ideas but none works here. So i did that...
I am using xampp last version on 2014.
go to \xampp\apache\conf\extra\httpd-xampp.conf.
we will find this bit of code:
<IfModule php5_module>
**<FilesMatch "\.php$">**
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
PHPINIDir "C:/xampp/php"
</IfModule>
Focus on second line, so we must to change to:
<IfModule php5_module>
**<FilesMatch "\.(php|html)$">**
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
PHPINIDir "C:/xampp/php"
</IfModule>
And that is it. Works good!
Simply you cant !! but you have some possbile options :
1- Excute php page as external page.
2- write your html code inside the php page itself.
3- use iframe to include the php within the html page.
to be more specific , unless you wanna edit your htaccess file , you may then consider this:
http://php.about.com/od/advancedphp/p/html_php.htm
Yes, you can run PHP in an HTML page.
I have successfully executed PHP code in my HTML files for many years. (For the curious, this is because I have over 8,000 static HTML files created by me and others over the last 20 years and I didn't want to lose search engine ranking by changing them and, more importantly, I have too many other things to work on).
I am not an expert -- below is what I've tried and what works for me. Please don't ask me to explain it.
Everything below involves adding a line or two to your .htaccess file.
Here is what one host ( http://simolyhosting.net ) support did for me in 2008 -- but it no longer works for me now.
AddHandler application/x-httpd-php5 .html .htm
AddType application/x-httpd-php5 .htm .html
That solution appears to be deprecated now, though it might work for you.
Here's what's working for me now:
AddType application/x-httpd-lsphp .htm .html
(This page has PHP code that executes properly with the above solution -- http://mykindred.com/bumstead/steeplehistory.htm )
Below are other solutions I found -- they are NOT MINE:
https://forums.cpanel.net/threads/cant-execute-php-in-html-since-ea4-upgrade.569531
I'm seeing this across many servers I've recently upgraded to EA4. Using cPanel Apache handlers or adding this directly in to .htaccess (same as cPanel does through gui add handlers):
AddHandler application/x-httpd-php5 .html
Sep 9, 2016
AddHandler application/x-httpd-ea-php56 .html
https://help.1and1.com/hosting-c37630/scripts-and-programming-languages-c85099/php-c37728/parsing-php-code-within-html-pages-a602364.html
Open a text editor such as wordpad, notepad, nano, etc. and add the following line:
AddHandler x-mapp-php5 .html .htm
If you want to use PHP 5.4 instead of PHP 5.2 then use the following line instead:
AddHandler x-mapp-php6 .html .htm
https://www.godaddy.com/community/Developer-Cloud-Portal/Running-php-in-html-files/td-p/2776
To run HTML using FastCGI/PHP, try adding this code to the .htaccess file for the directory the script is in:
Options +ExecCGI
AddHandler fcgid-script .html
FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .html
You can add additional lines for other file extensions if needed.
You need to make the extension as .php to run a php code
BUT if you can't change the extension you could use Ajax to run the php externally and get the result
For eg:
<html>
<head>
<script src="js/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajax({
url:'php_File_with_php_code.php',
type:'GET',
data:"parameter=some_parameter",
success:function(data)
{
$("#thisdiv").html(data);
}
});
});
</script>
</head>
<body>
<div id="thisdiv"></div>
</body>
</html>
Here, the JQuery is loaded and as soon as the pages load, the ajax call a php file from where the data is taken, the data is then put in the div
Hope This Helps
I'm not sure if this is what you wanted, but this is a very hackish way to include php. What you do is you put the php you want to run in another file, and then you include that file in an image. For example:
RunFromHTML.php
<?php
$file = fopen("file.txt", "w");
//This will create a file called file.txt,
//provided that it has write access to your filesystem
fwrite($file, "Hello World!");
//This will write "Hello World!" into file.txt
fclose($file);
//Always remember to close your files!
?>
RunPhp.html
<html>
<!--head should be here, but isn't for demonstration's sake-->
<body>
<img style="display: none;" src="RunFromHTML.php">
<!--This will run RunFromHTML.php-->
</body>
</html>
Now, after visiting RunPhp.html, you should find a file called file.txt in the same directory that you created the above two files, and the file should contain "Hello World!" inside of it.
<?php
echo '<p>Hello World</p>'
?>
As simple as placing something along those lines within your HTML assuming your server is set-up to execute PHP in files with the HTML extension.

How do I run PHP in HTML?

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

Categories