View Full Version : trying to use #include but I can't get it to work!
sucgar
12-17-2011, 04:41 AM
I'm trying to use includes to minimise maintenance. I have created the include file as linklist.shtml within a test file aptenia_test.html and uploaded both, but the links just don't appear. From what I've read this should be a simple thing to do. I'm not an html whizz.
Can anyone help? :confused:
The link is http://www.thesucculentgarden.com.au/aptenia_test.html
jscheuer1
12-17-2011, 05:11 AM
I've moved this to "Other" as includes are server side code, not HTML.
I'm unfamiliar with the type of include (shtml) you're using. The page you're importing to (aptenia_test.html) probably needs to be given the .shtml extension.
Try renaming it to aptenia_test.shtml
Other than that, as long as your server supports it (not all do), and you're using the right syntax, and the file you're trying to import is there, it should be fine.
See also:
http://www.htmlbasix.com/includes.shtml
I think the big question is whether your server supports SSI, and if you have enabled SSI.
Honestly, SSI is not widely used anymore. (I'm not sure how widely used it ever was.) Even the apache howto page isn't very enthusiastic about recommending it. All other tutorials or use cases I could find date back to 2002-2004 or earlier. Many reference/tutorial pages (at universities, etc.) are unmaintained or simply missing.
A more modern, fully featured language might be more useful to you (as well as offering a greater support base).
If you decide to use SSI, you might want to read the Apache howto:
..............http://httpd.apache.org/docs/current/howto/ssi.html
pay particular attention to the "Security Concerns" in the exec (http://httpd.apache.org/docs/current/howto/ssi.html#exec) section.
djr33
12-17-2011, 08:51 AM
Your server probably has PHP available (most do). If not, maybe ASP.
These languages are a lot more complicated than SSI, because they are more powerful. So I understand wanting to use "simpler and easier" SSI.
But, in reality, these languages actually are about the same difficulty level for simple operations.
In other words, you won't have to put in much more effort to use PHP and it will probably be better, certainly more standard. And while you're doing it you'll learn something about PHP which might help later.
For your information, here's some basic PHP code to include a page. Note that the page must end in the ".php" extension for it to work.
<!--HTML GOES HERE-->
<?php include('path/to/file.html'); ?>
<!--HTML GOES HERE-->
Easy! And there are lots of tutorials out there if you need more help (or you can post here again, of course). By the way, that's how I started using PHP, when I wanted to include some other files in my page.
molendijk
12-17-2011, 04:11 PM
Daniel is right: PHP is to be preferred to SSI using shtml. Shtml-files may 'loose' their include(s) after page-refresh, which is not what we want.
As a side note: when you still want to use shtml-files, only the includer-files should have that extension. Included files may very well be html-files.
===
Arie Molendijk.
molendijk
12-17-2011, 04:22 PM
The problem may be caused by the fact that you have the contents of the included files between <!-- --> (immediately after <!--#include FILE="linklist.shtml" --> ). Removing those contents may help. And are you sure the included file and the includer are in the same folder? If not, you should use <!--#include virtual="../linklist.html" -->
===
Arie.
jscheuer1
12-17-2011, 05:46 PM
The problem may be caused by the fact that you have the contents of the included files between <!-- --> (immediately after <!--#include FILE="linklist.shtml" --> ). Removing those contents may help. And are you sure the included file and the includer are in the same folder? If not, you should use <!--#include virtual="../linklist.html" -->
===
Arie.
SSI includes use that comment syntax:
<!--#include virtual = "some.txt" -->
But, as nearly everyone seems to be saying, PHP includes are better and do not use it.
I think your main problem is that the 'top' page (the includer) needs to be .shtml, not .html. But the server must support the SSI include, otherwise it makes no difference what code you use.
molendijk
12-17-2011, 06:59 PM
SSI includes use that comment syntax:
<!--#include virtual = "some.txt" -->
John, I know. I was not talking about that line, but about the comment lines BELOW that. The person repeats in comment lines what he has already included.
===
Arie.
sucgar
12-19-2011, 09:04 PM
Thanks everyone for your quick replies.
I've changed the include in aptenia_test.html to read
<?php include('linklist.php'); ?>
and the linklist extension.
Both have been uploaded, but it still doesn't work! Am I supposed to include something in the <head>? Obviously there's something I'm doing wrong.
jscheuer1
12-19-2011, 09:25 PM
Rename:
aptenia_test.html
to:
aptenia_test.php
sucgar
12-20-2011, 12:16 AM
Thankyou! Yes that works.
Excuse my ignorance - but is the .php extension likely to affect anything else ie customers/users won't have any problems? I'll have to change all my pages to .php.
jscheuer1
12-20-2011, 04:25 AM
There should be no problem renaming pages to .php, except that pages (if any) that were bookmarked as .html will no longer be there. You could leave a blank .html page behind for each though with a meta refresh tag on it that redirects to the PHP version. Or use .htaccess to redirect all calls to .html to their ,php versions.
BTW, the 'top' page (the including page, like aptenia_test.php) needs to have the .php extension, but the included page - like linklist.php does not.
djr33
12-20-2011, 10:23 AM
BTW, the 'top' page (the including page, like aptenia_test.php) needs to have the .php extension, but the included page - like linklist.php does not. Absolutely correct-- any pages that include active <?php /*PHP*/ ?> must have the .php extension.
On a technical note, I believe that adding the .php extension to the included page (the content) versus .htm will determine whether <?php ?> code blocks are parsed in the including page (the holder page) when that file's code is included. I'm not actually positive exactly how that works. But as a general rule: if you need PHP code from the second page to work in the first (in this case you do not), then use .php on both pages. If not, you can use .htm on the included page but always need .php for the including page. In general, though, I suggest using either .php or .htm consistently for the same content since that's easier to keep track of and allows for future use of any PHP code you might want, without the bookmarking problem John mentioned.
The .php extension is irrelevant. PHP code, by fundamental design, generates HTML code which is in every way identical to ".htm" HTML code. Then the proper headers are sent by your server so that the browser knows it's an HTML-type file (even though it's .php). In fact, you can rename any .htm file you'd like to .php and you won't see any changes at all.*
The only "difference" is the extension itself; all visitors will be able to view the page perfectly, but if you like the look and feel of the .htm ending, you won't get that with PHP. Here are three solutions, probably all overkill-- PHP is very common on the web-- look at the URL for the forum and you'll see .php there.
1) Tell your server to parse .htm files as .php. This could potentially slow the server very slightly (probably not noticeably), and the result would be that any PHP code on any .htm page would be parsed like that. As a security warning, be sure that no one without admin privelages (eg, FTP access) can upload a .htm file. Just like a PHP file then, it could be used to hack your site. (This isn't a security issue unless you allow public uploading.) If you want to do that, you can search for it based on your server's configuration (or let us know the details). You would need configuration access to change the settings.
2. Use .htaccess mod_rewrite (basically a way to "lie" to the visitor to use a fake URL to display a different page to mask all of the real .php files with .htm URLs. This puts very little burden on the server and it's not too hard to do, although for a beginner .htaccess is very difficult (in my experience). If you can find some code that's ready to go, though, it could be easy. Search around if you'd like.
3. My personal suggestion is to NOT use any file URLs. Instead, create index.php OR index.htm files and use directory names.
So your current site might have: mysite.com/mypage.htm. Instead, create an index.htm or index.php within the directory mysite.com/mypage/ and all of your pages will be very clean like that. It's a little bit of manual work (setting up all of those folders and storing all of the HTML files in different places), but the result is pretty URLs, and you also have a great added bonus that by referring to the URL of the directory rather than a file, it can refer to BOTH html and php, so bookmarks will always be valid for any type of file as the index page.
I hope that answers your question. Let me know if anything was unclear.
(*The only technical detail here is that because it's .php your server will check it for PHP code and try to run any PHP in it, so it might create a probably unnoticeable delay in serving the page-- a couple of milliseconds maybe. Unless you have an incredibly high traffic site, there's nothing to worry about. Either way, changing regular HTML files to PHP is certainly not required though.)
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.also, for using PHP without php file extensions:
#in your htaccess file
# (ALWAYS keep your existing htaccess file -
# add to it or comment lines out - NEVER overwrite it!)
AddHandler application/x-httpd-php .php .htm .html
# now all files with .php, .htm, OR .html will be parsed by php :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.