How Do I enable PHP to be shown on HTML webpage? - php

I have been searching on forums and google for hours with no definitive answer other than "Your host may not support PHP". I have dumbed down my PHP page to simply display some text for testing purposes but I am still only getting a blank webpage. I have tried opening it locally into a browser and also through the web server, same result for both. Anyone have an idea as to why this simple PHP wont even show up on the webpage?
<?php
echo "Show some text";
?>
also tried:
<html>
<?php
echo "Show some text";
?>
</html>

What is the filename of your file? Is the file extension a .html file, or a .php file? If it's .html, rename it to .php and test with your first test again.;
To check and see if php is supported on your server, create another file titled: phpinfo.php and insert the following code in it:
<?php
phpinfo();
?>
and save it, and then go to it on your server. You should see the PHP configuration output on that page. If it doesn't work, then you do not have PHP correctly installed on your server.

Your file must be ending in .phtml or .php to make PHP work on a HTML page.
index.php
<?php
echo "Hello World!";
?>
Or you can trick your server, Apache and Nginx support this as does IIS. Basically you can have "index.x" or "index.px" - your own custom extension, it is not advisable for obvious reasons. In Apache (your public_html folder) enable this in your .htaccess file:
AddType application/x-httpd-php .php
AddType application/x-httpd-php .ac1d
AddType application/x-httpd-php .php4
AddType application/x-httpd-php .phtml
AddType application/x-httpd-php .x
# your custom extension above.

To view the source of everything (no processing):
Most server are setup with a source-viewing format already by changing the file name to use the .phps extension. If not, you'd have to create a .htaccess file and temporarily add this line to it:
AddType text/plain .php
AddType text/plain .html
That should force the files to be displayed as plain text whenever they're accessed. Just remember to remove it later.

Related

Beginner / How to configure my .htaccess file so that it runs the PHP inside my HTML files?

So I'm trying to configure my .htaccess file so that it can runs the PHP code inside the HTML files.
When I go to mysite.com/thefile.php, it runs it correctly without any problem. Same if I go to mysite.com/thefile.html. But if I add <?php echo "Hello"; ?> inside the HTML file, it doesnt display anything. In fact, when I right click "View source code", it displays the php lines.
Here's what I tried with my .htaccess file:
RemoveHandler .html .htm
AddType application/x-httpd-php .php .html .htm
I also a tried with just
AddType application/x-httpd-php .php .html .htm
But it displays a "500 Internal Server Error" that says:
The server encountered an internal error or misconfiguration and was unable to complete your request.
The most common causes of this problem are errors in the app's .htaccess file or incorrect file or directory permissions preventing the web server from reading the .htaccess file.
More information about this error is available in the app's Apache error log at:
/srv/users/SYSUSER/log/APPNAME/APPNAME_apache.error.log
I'm quite new to web development in general, and I want to mention that, except from these 2/1 lines in the .htaccess file, I have nothing else. Maybe I'm missing some lines in it? Just a guess
Thanks guys (and gals if they exist!)
With Ubuntu/Apache 2.4.x, in your /etc/apache2/mods-enabled/php7.1.conf or equivalent, you could add:
<FilesMatch ".+\.html$">
SetHandler application/x-httpd-php
</FilesMatch>
(But you shouldn't do this at all! Use .php files for PHP code.)

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 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.

php will not run in my .htm page?

index.htm I have 6 lines of PHP code that wont run on my page I don't get an error of any kind it just wont run if you look in source it shows my raw PHP. I have a file named test.php that works fine. in test.php I only have those 6 lines of code that you can see in my index.htm
Server Name defiro
cPanel Version 11.36.2 (build 9)
Theme x3
Apache version 2.2.24
PHP version 5.2.17
MySQL version 5.5.30-30.2
Architecture x86_64
Operating system linux
i have tryed to fix the problem by putting AddType application/x-httpd-php .html in my websites .htaccess file as listed in PHP not working on HTML file i have tryed a few other things by trying to get an error out of the php with no luck
Edit__
it wont let me use php or html in a file together no matter if it is .html or .php it wont work i don't know how to fix it?
Let me see
index.htm I have 6 lines of PHP code that wont run on my page I don't get an error of any kind it just wont run if you look in source it shows my raw PHP. I have a file named test.php that works fine. in test.php I only have those 6 lines of code that you can see in my index.htm
Let me make some assumptions
index.htm has php tags inside (<?php echo 'if this works'; ?>) that do not parse when you make your request. The tags don't even show up in your browser.
test.php is a file that starts and ends with php tags (<?php /*php content*/ ?>). It runs as expected. (mind you, you can add content before or after php tags and it will be displayed raw).
If these assumptions are not correct please say so in a comment
Some other assumptions
you set up your own server or you haven't fiddled with the configuration yet
my suggestion
Go to your httpd.conf apache configuration file
Look for something that resembles the following
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
The one that's talking about php is the line you're interested in, it determines which file extensions will be parsed by the php interpreter before being sent to the client. OK, change it to look like this
AddType application/x-httpd-php .php .html .htm
You can add as many space-separated extensions you want, but be warned, sometimes you want a raw file, so be carefull
SAVE and RESTART the server. Important, everytime you change the conf
Please give feedback if this does not help you
you have put the following in
AddType application/x-httpd-php .html
That targets .html files, You need to target .htm files so changing to
AddType application/x-httpd-php .htm
should fix it

can't seem to get php working in MAMP

I'm trying to learn php and step one is getting php working in some capacity. I'm attempting to use MAMP but I'm having some trouble.
Specifically: if I create a file with the below code and save it as index.html in MAMP's "Document Root" directory, I get a blank page when pointing my browser at http://localhost:8888/index.html.
Code:
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
</head>
Alternatively, if I put a bit of php into its own file (say test.php) and then point my browser at this file, it just displays the full text of the file in the browser.
Any ideas what I might be doing wrong?
I had the similar issue.
Make a new file in TextWrangler or Komodo, or whatever, and add the folllowing code:
AddType application/x-httpd-php .html .htm
AddHandler application/x-httpd-php .html .htm
You're going to save the file as .htaccess (with the dot in the front; this is the file name).
Save it in /Applications/MAMP/htdocs. This is the same place you'll save your php and html files. This .htaccess will be an invisible file; you will not see it in Finder, tho you can if you cd to it in Terminal, or searching w/in Finder and choosing the File Visibility type under Kind.
Now try going to localhost:8888/ and you should see all of the available files there. And with this newly created .htaccess file, you can now embed php inside an html file too.
In MAMP, edit the file:
/Applications/MAMP/conf/apache/httpd.conf
and then search for '#AddHandler type-map' (exclude quotes). Below that, add,
AddHandler application/x-httpd-php .php .html
Save the file and stop and re-start MAMP. Php parsing will occur in files ending with the extensions: .php and .html.
You must save a file with PHP inside it with a .php extension. So you would need to name it index.php instead of index.html. Simple fix.
So, this just worked for me:
instead of having:
MAMP/htdocs/folder-that-contains-all-files/
put all your files directly in the htdocs folder!
so:
MAMP/htdocs/all your files including index.php etc.
Hope that helps!
Modifying /Applications/MAMP/conf/apache/httpd.conf
searching for #AddHandler type-map
and inserting AddHandler application/x-httpd-php .php .html
worked for me.

Categories