Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Server-side Scripting _ SSI includes - Apache

Posted by: ShadowyBob Oct 26 2006, 05:20 PM

I've sussed out how to add a line for including the date a document gets modified and how to format that output:

CODE
<!--#config timefmt="%A %d %B, %Y, %r" --><!--#flastmod file="aatest.htm" -->
I also know that there is an
CODE
echo var="DATE_GMT"
to get a date into GMT format. What I can't figure is how to combine the two, nor how to change GMT to BST. Help!

Posted by: Christian J Oct 26 2006, 05:34 PM

Maybe you can find something about SSI here: http://bignosebird.com/sdocs/echo.shtml

Otherwise I suspect PHP is better suited for things beyond mere inclusions. ;-)

Posted by: ShadowyBob Oct 26 2006, 05:55 PM

Thanks Christian J, I've now amended my

CODE
<!--#flastmod file="aatest.htm" -->
to
CODE
<!--#echo var="LAST_MODIFIED"-->
but even that link doesn't mention how to combine two variables.

From this and other postings, quite a few of you have now mentioned that I should be using php for my various 'includes'.

Now my very basic understanding of php says I can include htm docs in a php document but not the other way round. Can you please either explain or point me in the direction of a simple tutorial?

Posted by: jimlongo Oct 26 2006, 06:22 PM

QUOTE(ShadowyBob @ Oct 26 2006, 06:55 PM) *

From this and other postings, quite a few of you have now mentioned that I should be using php for my various 'includes'.

Now my very basic understanding of php says I can include htm docs in a php document but not the other way round. Can you please either explain or point me in the direction of a simple tutorial?


You basically are gong to including php functions within an html document. You will use these functions to include other html or add your time and dates, etc.,
The only monkey wrench in this equation is whether you have to call your documents webpage.php or webpage.html and that depends on your server setup.

Here's the simple PHP starter tutorial that many people start with
http://ca3.php.net/tut.php

Posted by: ShadowyBob Oct 26 2006, 06:57 PM

QUOTE(jimlongo @ Oct 27 2006, 12:22 AM) *
The only monkey wrench in this equation is whether you have to call your documents webpage.php or webpage.html and that depends on your server setup.

Here's the simple PHP starter tutorial that many people start with
http://ca3.php.net/tut.php


Thanks for this jimlongo.

Queries:
  1. Is there a simple test page I can use to determine if my server can process php from html files
  2. Thanks for the tutorial link. I can create basic php pages and cosmetically edit existing ones, but can find no examples anywhere of how to include php within an html document
Do you have any examples of how php can be used to include headers in numerous html pages and how to include a file modified date on all my pages?

Posted by: jimlongo Oct 26 2006, 07:24 PM

here's how to include one html document inside another using php -> here's a header and a menu inside their own CSS DIV's

CODE

<body>
<!--HEADER-->
<div id="header">
<?php        
include( 'mypages_header.html' );
?>
</div>
<!--MENU-->
<div id="Menu">
<?php        
include( 'mypages_menu.html' );
?>
</div>



Posted by: jimlongo Oct 26 2006, 07:59 PM

QUOTE(ShadowyBob @ Oct 26 2006, 07:57 PM) *


Is there a simple test page I can use to determine if my server can process php from html files


put this in a document and call it phpinfo.php
CODE
<HTML>
<?php phpinfo(); ?>
</HTML>

Then retrieve it with a browser, it should show you a long list of your php installations configuration


----
Then change the name of the document to php_info.html and see if it still works. If not then you need to change something on your server, which is easy if you have control panel access or can put an .htaccess file on an Apache server.

Posted by: ShadowyBob Oct 27 2006, 09:25 AM

jimlongo, just what I needed! Thanks ever so much.

I've done as you said and created the phpinfo.php with the php instructions inside the <html> tags. This works fine just like an ordinary phpinfo.php file. I then changed the file extension to .html - no luck :-(

I already have an .htaccess.html file with Options + Includes in. What would I have to include to get the html/htm files to process php commands? (NB, I already have a sub-domain running a phpbb forum, so wouldn't want to affect that!)

Finally, I understand very clearly your instructions for a file inclusion, thanks. Any suggestions for how to include a file-modified date/time showing GMT/BST info. (my server's time is currently showing CEST, which is one hour more than BST)?

Posted by: jimlongo Oct 27 2006, 09:33 AM

To be able to run php within an html document you need to add the following directive in your .htaccess file

CODE
AddType application/x-httpd-php .php .php4 .php3 .phps

Posted by: Peter1968 Oct 27 2006, 10:30 AM

QUOTE(jimlongo @ Oct 28 2006, 12:33 AM) *

To be able to run php within an html document you need to add the following directive in your .htaccess file

CODE
AddType application/x-httpd-php .php .php4 .php3 .phps



Which, if Shadowybob is running phpBB, he'd already have, or it'd be stated in httpd.conf. I suspect the latter.

To include the date within your page you'd add
CODE
<?php date(parameters); ?>
in the place you'd want to insert it, and the page will need the php extension for it to be parsed correctly.

The date() command has a multitude of options.
http://www.php.net/manual/en/function.date.php

Posted by: ShadowyBob Oct 27 2006, 10:32 AM

jimlongo, thanks.

To re-cap, I now have a file phpinfo.html containing the following:

CODE
<html>
<?php phpinfo(); ?>
</html>

and an .htaccess.html file containing
CODE
Options + Includes
AddType application/x-httpd-php .php .php4 .php3 .phps


Sorry jimlongo, but when I browse to phpinfo.html all I get is a blank screen:-(

If I get this to work, I think I've now found out how to do a file-modified date:
CODE
<?php echo "Last modified: " . date( "F d Y. H:i:s a", getlastmod() ); ?>
All I need is a time-zone command that works.

Posted by: Christian J Oct 27 2006, 12:00 PM

QUOTE(jimlongo @ Oct 27 2006, 04:33 PM) *

To be able to run php within an html document you need to add the following directive in your .htaccess file

CODE
AddType application/x-httpd-php .php .php4 .php3 .phps



The above makes the PHP engine look for PHP scripts in files ending with the specified extensions .php .php4 .php3 .phps. To make it work with .html extensions you must add that one too. You can probably delete the rest if you're not going to use them anyway, but maybe it's good to keep at least .php (like if you include a .php file in a .html file)?

CODE
AddType application/x-httpd-php .php .html

Posted by: ShadowyBob Oct 27 2006, 12:41 PM

Christian J, makes perfect sense.

To avoid typos, I copied and pasted your code into my .htaccess.html, but still a blank screen when I browse to phpinfo.html :°°°°-( (my feeble attempt at tears)

Posted by: Christian J Oct 27 2006, 03:23 PM

QUOTE
.htaccess.html


The file is just called ".htaccess", nothing more.

Posted by: ShadowyBob Oct 27 2006, 05:33 PM

Thanks again Christian J. I thought I'd tried this some time ago, but just to check, I renamed it to .htaccess.

Then I saw why I'd abandoned that idea!

QUOTE
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, cfWEBADMIN and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

??

Posted by: Christian J Oct 27 2006, 05:37 PM

Oops. Sounds like you should contact them. tongue.gif

Posted by: jimlongo Oct 27 2006, 05:41 PM

Sounds like you're not allowed to place .htaccess files, many servers don't allow that.

Do you have a Control Panel? Very oftern you can set these options there.
That's how mine are set, as is evidenced by the incomplete apache directive i gave you.

Posted by: ShadowyBob Oct 27 2006, 07:41 PM

Yes jimlongo, I have a Control Panel, but have looked in vain for anything that looks like I can ask for .htaccess or anything else.

However, I noticed that I can password-protect my directories, so I did just that. Guess what – an .htaccess file was added to my web site!

I then ftp'd that to my PC, returned to the Control Panel and removed the password-protection. Interestingly it left an empty .htaccess file behind.

I removed everything from the downloaded .htaccess file, inserted

CODE
AddType application/x-httpd-php .php .html
and ftp'd it back to the server.

This time, when trying to open phpinfo.html, my browser downloaded it to my PC instead of viewing it! (the downloaded file is just a blank screen with a source code of
CODE
<html>
<?php phpinfo(); ?>
</html>


Does this never end?

Posted by: jimlongo Oct 27 2006, 08:16 PM

Nice try Shadowy Bob! Very resourceful of you smile.gif

Look in the cPanel Web Options or something like that, look for anything PHP.

Posted by: ShadowyBob Oct 27 2006, 08:36 PM

Didn't I do enough? And if so, what did I do wrong?

The only thing in my Control Panel that looks anything like, is a thing called Directory Manager. Inside this are three options, all of which automatically produce a completed .htaccess file. They are: 'Password Protection', 'IP Filtering', and 'Lock Access'. I can't find any other reference to a .htaccess file. There is a 'Script Scheduler' within a PHP/MySQL section, but this does not seem to be anything I would know how to use, nor does it seem relevant.

Posted by: jimlongo Oct 27 2006, 09:10 PM

I think you might be caught up with the term .htaccess . . . for instance in my Cpanel the directives for PHP make no reference to .htaccess, they try to make it user friendly by eliminating the magic behind the curtain.

Like Christian said, the shortest route may be just to contact your provider. Tell them you want to enable php to be parsed within html documents. It's a pretty common request.

Posted by: Brian Chandler Oct 27 2006, 10:39 PM

QUOTE(Christian J @ Oct 28 2006, 02:00 AM) *

QUOTE(jimlongo @ Oct 27 2006, 04:33 PM) *

To be able to run php within an html document you need to add the following directive in your .htaccess file

CODE
AddType application/x-httpd-php .php .php4 .php3 .phps



The above makes the PHP engine look for PHP scripts in files ending with the specified extensions .php .php4 .php3 .phps. To make it work with .html extensions you must add that one too. You can probably delete the rest if you're not going to use them anyway, but maybe it's good to keep at least .php (like if you include a .php file in a .html file)?


No, you don't need the extensions of files that will be included. The .htaccess thing tells the server to pass http requests for these types of file through the PHP interpreter - but once the PHP interpreter is looking at a file, it looks at everything, including 'included' files of whatever type or flavour.


Posted by: Christian J Oct 28 2006, 07:03 AM

QUOTE(ShadowyBob @ Oct 28 2006, 02:41 AM) *

CODE
<html>
<?php phpinfo(); ?>
</html>



As a side-note I don't think you should use those HTML tags, the phpinfo() function creates all the HTML.

Posted by: ShadowyBob Oct 28 2006, 08:43 AM

Thanks Christian J. As another aside – does it matter if a file has the extension .htm as opposed to .html?

jimlongo, you're right, I think I'll have to short-circuit all this by using the £1 a minute phone call (but, of course, not available at weekends)!!

Brian, so are you saying all that is needed is

CODE
AddType application/x-httpd-php
?

Posted by: jimlongo Oct 28 2006, 09:54 AM

QUOTE(ShadowyBob @ Oct 28 2006, 09:43 AM) *

Thanks Christian J. As another aside – does it matter if a file has the extension .htm as opposed to .html?

jimlongo, you're right, I think I'll have to short-circuit all this by using the £1 a minute phone call (but, of course, not available at weekends)!!

Brian, so are you saying all that is needed is
CODE
AddType application/x-httpd-php
?


I'm no expert, but by my reading of the Apache docs http://httpd.apache.org/docs/1.3/mod/mod_mime.html#addtype you do need to add the extensions of filetypes you want parsed for php. If you want to have php parsed inside of html documents you need to add it to the directive. If you want to use .htm as well as .html then add that
CODE

AddType application/x-httpd-php .php .html .htm  

Posted by: Christian J Oct 28 2006, 10:28 AM

QUOTE(ShadowyBob @ Oct 28 2006, 03:43 PM) *

Thanks Christian J. As another aside – does it matter if a file has the extension .htm as opposed to .html?


No, not as long as .htm is specified.

QUOTE
Brian, so are you saying all that is needed is
CODE
AddType application/x-httpd-php
?


I think Brian meant that you don't need to specify the extensions of included files (but you must still specify the including files). So this:

CODE
AddType application/x-httpd-php .html


will switch on the PHP engine for .html files. Then if e.g. foo.html contains

CODE
<?php include('foo.txt'); ?>


foo.txt will be included, even though .txt is not specified in the AddType directive. You can even put PHP inside foo.txt:

CODE
<?php echo 'This is in foo.txt'; ?>


and it should run in foo.html.

Posted by: ShadowyBob Oct 28 2006, 08:12 PM

Thanks jimlongo and Christian J. I'll report back after having a word with my server help desk.

Posted by: Brian Chandler Oct 29 2006, 12:53 AM

QUOTE(Christian J @ Oct 29 2006, 12:28 AM) *


I think Brian meant that you don't need to specify the extensions of included files (but you must still specify the including files). So this:

CODE
AddType application/x-httpd-php .html


will switch on the PHP engine for .html files. Then if e.g. foo.html contains

CODE
<?php include('foo.txt'); ?>


foo.txt will be included, even though .txt is not specified in the AddType directive. You can even put PHP inside foo.txt:

CODE
<?php echo 'This is in foo.txt'; ?>


and it should run in foo.html.


Not only "should", it will. And not only you can put '<?php' inside the included file, anyone can. So an important security notion is that you must never use "include" on anything that might have been supplied by a user.


Posted by: ShadowyBob Oct 29 2006, 11:12 AM

QUOTE(Brian Chandler @ Oct 29 2006, 05:53 AM) *
Not only "should", it will. And not only you can put '<?php' inside the included file, anyone can. So an important security notion is that you must never use "include" on anything that might have been supplied by a user.


Excellent tip – thanks Brian

Posted by: ShadowyBob Nov 6 2006, 08:36 AM

Right - after what seems an age, I'm back with some more information and would love some more help please:

1. Information from server help desk -

QUOTE
...your www folder is the top level your programs can run from and you may require to amend your scripting to reflect this...

2. If I create an .htaccess file on my root directory with the line
CODE
AddType application/x-httpd-php .html .htm
all the pages with
CODE
<!--#include virtual="/included/foo.htm" -->
show an error message instead of the included file.
3. If I add the line
CODE
Options + Includes
to the .htaccess file, I get the Internal Server Error message.

Any suggestions?

Posted by: Peter1968 Nov 6 2006, 03:49 PM

It's Options +Includes (no space there), and in the AddType directive, get rid of .htm and .html, put .shtml there instead and try naming your SSI-enabled files with the .shtml extension.

Failing this, google for the XBitHack method.

Posted by: Christian J Nov 6 2006, 03:58 PM

QUOTE(ShadowyBob @ Nov 6 2006, 02:36 PM) *

2. If I create an .htaccess file on my root directory with the line
CODE
AddType application/x-httpd-php .html .htm
all the pages with
CODE
<!--#include virtual="/included/foo.htm" -->
show an error message instead of the included file.


Doesn't
CODE
AddType application/x-httpd-php .html .htm
apply to PHP exclusively? But
CODE
<!--#include virtual="/included/foo.htm" -->
is SSI, not PHP. Making both SSI and PHP work on the same page is tricky if at all possible (can't quite remember).

Posted by: Peter1968 Nov 6 2006, 04:10 PM

CODE
AddType application/x-httpd-php .html .htm


Ack, should've seen that. My bad

Posted by: ShadowyBob Nov 6 2006, 08:30 PM

Thanks Christian J and Peter1968.

Re .htaccess, I got rid of the space between the '+' and the 'Includes' - made no difference - still the Internal Server Error. I then removed the 'AddType...' declaration and the site was again accessible. It certainly seems that the two don't go down well together!

I reckon I'm going to stick with the SSI 'includes' - at least I know it works (and it doesn't need the .htccess file)!

Do I now take it as a certain that the two can't be mixed or is there somewhere I could go to find out?

Posted by: jimlongo Nov 6 2006, 10:00 PM

They should work together.

Since you say you can use SSI without the .htaccess why not try it with just the AddType declaration for php in the .htaccess file and see if that works. Cause the Includes line is not needed for php parsing.

Posted by: Christian J Nov 7 2006, 08:48 AM

QUOTE(ShadowyBob @ Nov 7 2006, 02:30 AM) *

Do I now take it as a certain that the two can't be mixed


They might work if you use .shtml and/or .php extensions. Perhaps you can use .html for one but not the other. But I haven't been able to make both SSI and PHP work with .html extensions (yet).

Posted by: ShadowyBob Nov 8 2006, 04:39 PM

Thanks jimlongo, but see my post above:

QUOTE(ShadowyBob @ Nov 6 2006, 01:36 PM) *
...
2. If I create an .htaccess file on my root directory with the line
CODE
AddType application/x-httpd-php .html .htm
all the pages with
CODE
<!--#include virtual="/included/foo.htm" -->
show an error message instead of the included file.
...


Thanks also, Christian J – if you manage it let me know! In the meantime I'll keep looking and experimenting.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)