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
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'm familiar with HTML but only just starting to learn php. I can't seem to figure out why this code isn't working. I've previously had no problems with include()'s but that is when the php code is it's own distinct file. This time around I'm trying to create a dynamic web page with php scripts. I have apache running with xampp and I have the gf.css file and testing.php in the same folder as the main html file.
The problem I can't seem to figure out is that nothing is showing up in the text box when I try and run the HTML page. It seems as though nothing in the php tags is running. I put in echo statements to try and determine what the problem was but nothing showed up unless, however, I placed a h1 tag after the first " in my echo statement. But inn this case, no matter where I placed the closing h1 tag, the rest of the php script just printed. It was as if I couldn't close the tag.
That's as much info as I can give really. Any help and/or explanations as to what I'm doing wrong would be much appreciated! Thanks in advance.
Here's the HTML file
<html>
<head>
<link rel='stylesheet' type='text/css' href='gf.css'>
<body>
<h1>This is my web page</h1>
<div id='main_box'>
<?php
include ('testing.php');
?>
</div>
</body>
</head>
</html>
Here's the .css file
#main_box
{
height:500px;
width:400px;
color:white;
background-color:1e1e1e;
}
And here's the testing.php file if that matters
<?php
echo "test page";
?>
If you want to write php code then file extension must be ".php". So change your main HTML file extension to PHP.
First Solution:
If you want to write php code then file extension must be ".php". So change your main HTML file extension to PHP.
Second Solution:
You can tell Apache to treat .html files as PHP by adding a new FilesMatch directive:
1. Open C:/xampp/apache/conf/extra/httpd-xampp.conf
2. Search:
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
3. Add those three line:
<FilesMatch "\.html$">
SetHandler application/x-httpd-php
</FilesMatch>
Third Solution:
You can do it by adding an .htaccess file into your document root that contains:
AddHandler application/x-httpd-php .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
I'm looking for a method to suspend access to a website without loosing the old files, by adding a file to the directory but not .htaccess, since the use of .htaccess has been disabled on the specific server. Are there other ways to do this by using php or another method?
rename your www-root directory and add another, empty one.
You have to rename your index.php, create a new one, and add:
<?PHP header("Location: http://disney.com"); // or something ?>
Also, you may want to check Apache's DirectoryIndex directive, the order of the "default page" is defined here. If your server have DirectoryIndex index.html index.php, the html extension is prior to the php, so You can create a file index.html with:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Jump page</title>
<meta http-equiv="REFRESH" content="0;url=http://disney.com"></HEAD>
<BODY>
Jumping
</BODY>
</HTML>
But if You want to "remove" all files recursively, You have to move the whole directory. Another option is to rewrite the url http://yourpage.com/hidden_directory/ to somewhere, but sadly You need .htaccess to do that, if You can't access the webserver's config file. If You can:
<Directory /path/to/dir/>
RewriteEngine On
RewriteRule ^/hidden_directory/$ http://disney.com [L}
</Directory>
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.