This feels like an extremely n00b question, but here goes...
I have a series of HTML files with a small amount of HTML content inside each (exported from a live system). Its not feasible to change file extensions, as I will not be the person performing this 'export - burn to CD' process when I hand my project over.
Here's a sample page, its extremely basic, it is "01.html":
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../css/styles.css" />
<title>Introduction</title>
</head>
<body>
<h1 class="chaptertitle">Introduction</h1>
<div>
<p>FBT is imposed on non-cash benefits provided to employees in addition to their normal cash salary. The main categories of fringe benefit for Administrator User are:</p>
<ul>
<li>The private use, or availability for private use, of an employer’s motor vehicles;</li>
</ul>
</div>
</body>
</html>
The files are linked to from a main page, everything works fine in that regard - but I am sure I've seen pages with a .html extension parse PHP syntax, how can I enable/ do this? Currently, I'd thought I could do a search/replace on things like 'Administrator User' in the above example to something like:
<?php echo $CFG->username; ?>
...where that variable is defined and available - but the page is being parsed as HTML. In some ways this isn't a surprise, in some ways it is, as like I say I'm sure I've seen PHP code parsed in a file with a .html extension.
It is running on a Server2Go stack (for burning to CD), but during testing it is running on a WAMP stack. I'm not able to modify much about Server2Go.
Ack! Sorry, to clarify:
everythign inside the <body> tag is pulled out and used in a PHP context, a page named 'generate_book.php' which displays the pulled out contents as the PHP pages' own <body> tag.
Sorry, that was somewhat important :P
Well I'm rather sure you're not very accustomed to the way the apache engines works with php. In truth, this isn't a php question but an apache one (in case you're using apache, which I will assume)
The simplest way of doing this, is to add a .htaccess with the following contents:
AddType application/x-httpd-php .php .html
This basically tells apache that all .html files should be parsed by PHP, read more about it here.
What you actually saw, I suspect are URL rewrites, read more about mod_rewrite.
If you can't modify the server's setup, then you're SOL. You have to tell the server that .html files should be treated as PHP scripts. There's no way around this. It won't magically start sending them through the PHP parser unless you tell it to.
On an Apache setup, it's a matter of putting in a configuration directly, such as
AddType application/x-httpd-php .php .htm .html
ModRewrite
RewriteEngine on
RewriteRule ^(.*).html$ $1.php
AddType
AddType application/x-httpd-php .htm .html
I worked around the problem by adding a config variable which is then used in a string replacement call to replace an export-generated string with one which someone can update in a config file, before burning to CD. I can get away with this because the string replacement is essentially the only PHP in these HTML files. I'll close this question after... leaving for anyone to check up on/read about quickly.
Related
Given the following index.html file:
<html>
<body>
<p>Welcome to <?= $_SERVER ['HTTP_HOST']; ?></p>
</body>
</html>
I expect to see "Welcome to EXAMPLE.COM", All I see is "Welcome to ".
What would cause this to happen if the code checks out?
Your file is named index.html. Unless you told your server that .html files should be treated as PHP scripts, that means the PHP code is NOT being executed - it's going out as literal text. And since PHP tags make it look like HTML, your browser is properly hiding that unknown/illegal tag.
Rename it to index.php.
It's not running because that part is not html (it's php) but you have saved it as an html file instead of as a php file.
rename it to index.php and try again?
Web servers are usually configured to run PHP only on files with the .php extension. Your index.html file will be passed as-is to the browser, which will probably ignore the unknown PHP tags. If you take a look at the source code of the web page, the tags will probably be there.
If you must have a .html extension, you can usually configure the web server to run PHP on .html files. For example, in Apache, you can use the AddType directive in an .htaccess file or in the server configuration (httpd.conf):
AddType application/x-httpd-php .html
However, this will run PHP on all .html files (in that directory), which may put an unnecessary load on the server.
A much better way is to use URLs without extensions. In Apache, you can use the DirectoryIndex directive to specify a list of index files that the web server will search for:
Options +Indexes
DirectoryIndex index.html index.php
When a browser requests a URL that ends with a slash, such as http://mydomain.example/foo/, the server will search for foo/index.html or foo/index.php in the DocumentRoot (or, failing both, generate a directory listing). You can now use whichever type of index file is appropriate for the moment, without ever having to change your URLs.
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
I've searched all over the internet and can't seem to find a solution to my problem. I want to be able to "call" a php file from an html file and display the string returned:
html_ONLY_file.html
...
<h1>GetHeader.php?type=main</h1>
...
GetHeader.php
if($_GET['type'] == 'main')
print 'Some header to display'; //or echo 'Some...';
exit;
I've done this for images, img src="image.php?file=file.jpg", where image.php does a header(...) and readfile(...) return but I do not know how to do this for simple text. I'm not looking for DOM or anything too involved, if I have to I will. I want to know if there is a simple solution. It's generating the html side that I'm lost on.
In case you want to know, I am doing this because I once used a <#virtual include=...> to call for a php file from my .shtml file. Well, the hosting company decided to change mod_security and now I cannot include any php files (I've already tried everything). Including html files works fine and so I am changing this part of the website around so I don't have to rename files because the site is for a small business that is now ranked highly on Google for its geographical area. If I change the file names, shtml to php, then I believe the Google ranking drops (don't comment on this part unless you are damn sure you are 100% correct).
If you can edit the .htaccess file you can add a line in your .htaccess file that will mean HTML files will be parsed as if they were PHP.
If your server is running PHP as an apache module:
AddType application/x-httpd-php .html .htm
If your server is running PHP as CGI:
AddHandler application/x-httpd-php .html .htm
Source: http://www.besthostratings.com/articles/php-in-html-files.html
Once you've added that your html_ONLY_file.html could look like:
<h1><?php print "some header"; ?></h1>
And it would function just as if it were a .php file.
Alternatively, you could convert all your files to .php and add redirects into the .htaccess file like so:
rewriteRule ^somefile.shtml$ somefile.php [R=301,L]
rewriteRule ^another-file.shtml$ another-file.php [R=301,L]
This effectively says to your server "whenever a user requests somefile.shtml, act as if they requested somefile.php instead". The R=301 is the most important part with regards to Google rankings. This tells Google (and anyone who requests the .shtml file) that it's permanently moved to the new location somefile.php. This transfers all / almost all of the ranking power from the old location to the new location.
Source: http://moz.com/learn/seo/redirection
I'm working on a navigation bar similar to this website and google, and lets say i would like to make more sites with the same layout. How would i go about doing this with html? I know that php has:
<?php include "file name"; ?>
Is there a way to do something like that in HTML document?
HTML is not a server-side language, it's just a markup language. It does not offer any way to include the contents of a different page in your HTML code. HTML simply doesn't support it.
If you're willing to use a library such as jQuery, then this will be an easy task. You can use the jQuery .load() method to fetch the contents of your HTML file and then inject it the HTML DOM.
$(document).ready(function() {
$('#someDiv').load('/path/to/file.html');
});
Note that remote files won't work because of the Same Origin Policy. Also, note that it's a bad idea if you care about SEO. The search engine spiders won't see your actual HTML code, and they might miss out important parts of your website.
The best way to do this would be to use a server-side language. If you're already serving dynamic content, then it makes more sense to do so. Server-side scripting runs on the server machine and then the results of that scripting, generally HTML markup are then sent out to the client.
The difference is that the HTML markup is generated before pageload, not during or after, as is the case with the method shown above.
Just make your HTML files run through a PHP parser, I do that for all my sites so things like menus and other repeating sections can just be updated once. You do this with your .htaccess file.
This will make all .htm and .html files be run through the PHP parser as well.
<FilesMatch "\.(htm|html)$">
SetHandler application/x-httpd-php
</FilesMatch>
Then you just use the same method you mentioned in your original post. Whatever you include will be processed by the PHP parser, then sent to the browser, which will interpret the HTML.
Note that PHP files can hold anything in them, only the parts inside <?php ?> tags will actually be processed by the PHP parser. So I have section that are all HTML, except for one small section that has something like a year. The below is how my websites will never have their copyright be out of date, even if the website is not maintained.
<p>
Copyright<sup>©</sup> NAME. All Rights Reserved. <?php echo date("Y"); ?>.
</p>
"Is there a way to do something like that in HTML document?"
Yes there is an HTML equivalent (if I can say) to include files, and it's called Server-side Includes (SSI) which uses the .shtml file extension, however this is an Apache feature. Yet, from a recent finding, it can run on Windows servers. Consult Microsoft's Developer Network for more information on the usage of SSI and this page also on Microsoft's IIS.net.
The syntax is: (no space between the <!-- and the #include...)
<!--#include virtual="/includes/file.shtml" -->
This would look and fetch a file called file.shtml to the root and inside the "includes" folder.
Note: This can be any folder you want it to be.
You can also include different file extensions such as .htm .html .txt and even another .shtml file.
I manage a few Websites with that particular file extension, and the benefit of including .shtml files, is that you can also do more includes inside those, but not with the other file extensions I already listed.
However, there is an exception to this rule. You can tell the server to treat .html or .htm to run as .shtml just as long as you use the
AddHandler server-parsed .html
AddHandler server-parsed .shtml
AddHandler server-parsed .htm
Apache commands inside an .htaccess file and placed in the root of your server.
Back to the matter at hand. You can use it anywhere in the document you wish to include a file in.
For more information on Server-side Includes and other available options, visit the Wikipedia Website, or Google the term "Server-side Includes (SSI)".
http://en.wikipedia.org/wiki/Server_Side_Includes
<frameset cols="25%,50%,25%">
<frame src="www.google.com">
<frame src="www.facebook.com">
<frame src="www.something.ocm">
</frameset>
FRAMETAG is not supported in HTML5! Beaware
In W3C Validation it will throw an error, but in real it will work.
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.