homepage Welcome to WebmasterWorld Guest from 54.204.249.184
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Including php in shtml
thirt33n




msg:4552006
 8:13 am on Mar 7, 2013 (gmt 0)

Hi,

Can anyone advise re: inclusion of a php script in multiple .shtml pages. I have a shtml based website and want to include a comments/discussion php script on pretty much all the pages.

At the moment I have headers/footers etc. included on my shtml pages as..

<!--#include virtual="footer.html"-->

etc. And ideally I'd like to do the same with comments.php but I don't know how to do this. I already use php on the site successfully on my contact form page and when I test domain.com/comments.php the php comments are working/loaded successfully but it's the addition to my shtml pages that I can't seem to do.

Thanks in advance for any replies.

 

lucy24




msg:4552007
 8:44 am on Mar 7, 2013 (gmt 0)

What have you tried so far? What happened?

I assume you're on shared hosting. This means that you may need to get some more information from their end to know exactly what settings to change.

In my case f'rinstance I didn't have to change anything. I just shoved a php file into the "include" line and it worked. Fortunately nobody had told me that it might not ;) Well, except that my system doesn't like "include virtual" followed by a relative URL. It insists on a leading slash to make it site-absolute. (I think technically it went the other way around. Relative links work with "include" alone; absolute links need the "virtual" bit.)

Now, including other stuff inside php... that gets tricky.

topr8




msg:4552054
 10:44 am on Mar 7, 2013 (gmt 0)

can i just backtrack you a minute ... how far into your project are you?

is there a reason why you are using shtml?

i'd suggest dropping shtml altogether, just use php pages. (you can include files within php no problem)

Lexur




msg:4552058
 11:06 am on Mar 7, 2013 (gmt 0)

First of all, as topr8 says, it must be a powerful reason to still maintan the .shtml files (I suppose is Google rankings); otherwise you will change the next minute to php or directly to Wordpress.

That what you want to do needs two steps:

1.- Include your php script this way:

<?php include '../commentsdir/comments.php' ; ?>

2.- If that doesn't work, as we don't know how old your server's setup is, you could try to add a .htacces file to the directory where the files are in with this content:


Options Includes
AddType text/html .shtml
AddHandler server-parsed .shtml


Hope it works.

g1smd




msg:4552062
 11:40 am on Mar 7, 2013 (gmt 0)

You can swap the site to use PHP functionality and still retain .shtml URLs if you need to.

thirt33n




msg:4552094
 1:37 pm on Mar 7, 2013 (gmt 0)

Thanks for the replies. I've tried the suggestions here but no joy (yet!) if it helps any the server is running php v5.3.22.

Re: Rewriting my shtml urls to php ones - um, no thanks! The website is an 8yr old ecommerce site and I'd think I'd rather do without the comments/interaction addition to it than 301 everything.

Also Wordpress was mentioned in the replies - this isn't for a blog. It's the Disqus api (vs their javascript) I want to include on my product pages.

g1smd




msg:4552154
 3:30 pm on Mar 7, 2013 (gmt 0)

I said rewrite, not redirect.

With a redirect, you'd have all new .php URLs and would redirect .shtml requests to those new URLs.

With a rewrite, you would continute to use your old .shtml URLs, rewriting the request to be handled by a .php file.

Rewriting does not make new URLs. If you think that, you have the whole process "exactly backwards" in your head. Rewriting takes the .shtml URL request and looks for a non-default internal location to serve the content (i.e. a .php file).

lucy24




msg:4552301
 9:29 pm on Mar 7, 2013 (gmt 0)

Include your php script this way:
<?php include '../commentsdir/comments.php' ; ?>

Or rather, not this way, since the last thing you want is hard-coded relative links in a part of your html that you con't plan on changing often.

If you are lucky, there will be no difference between

<!--#include virtual="/directory/filename.php" -->


and

<!--#include virtual="/directory/filename.html" -->


as includes within the original (s)html file.

Careful! There's one format that only works if your server has mod_php. (MAMP does; my real server doesn't.) I think it was the simple "include" as opposed to "include virtual"; all I know is, I had to get rid of it everywhere.

Includes called from within php currently use the form

include ($_SERVER['DOCUMENT_ROOT'] . "/directory/filename.php");

If there exist common configurations where this would not work, I hope someone will tell me ahead of time.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved