Results 1 to 24 of 24
  1. #1
    Newbie Adrian's Avatar
    Join Date
    December 30th, 2005
    Location
    Santa Cruz, California
    Posts
    41
    what php include code do you use and...why?
    I just started by using:

    <!--#include file="file_name.php" -->

    but others use:

    <?php include("file_name.php"); ?>

    I'm not sure what the difference is.

    Plus there's a variation on the first:

    <!--#include virtual="file_name.php" -->

    The reason I ask is that I am getting ready to roll out a large site using includes and want to start out using the best one for my purpose.

    Thanks

  2. #2
    ABW Ambassador Nature Boy's Avatar
    Join Date
    January 18th, 2005
    Location
    Tennessee
    Posts
    1,423
    This is used on .shtml pages within the same directory:
    <!--#include file="file_name.php" -->

    This is used on .php pages within the same directory:
    <?php include("file_name.php"); ?>

    This is used on .shtml pages to call a file in another directory:
    <!--#include virtual="file_name.php" -->typically it would look like this:
    <!--#include virtual="/another_directory/file_name.php"-->

    If your regular pages end in .shtml, I would suggest using <!--#include virtual="/another_directory/file_name.php"--> and putting files like your header, footer, and navigation in that directory. It makes for easy maintenance down the road.

    If you regular pages end in .php, I would suggest using
    <?php virtual ("/another_directory/file_name.php"); ?> for the same reason.
    Scott
    If you can't dazzle them with brilliance, then baffle them with bulls#!t
    Don't tell me that you'll do it... SHOW ME.
    Just because everyone else is drinking it is no reason for me to drink the KOOL-AID.

  3. #3
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    Definitely use the <?php include(); ?> function. The old way to do it is to use the <!--#include file--> statement which is Server Side Includes (SSI). SSI pre-dates PHP and is obsolete in some web hosts so I don't recommend it. It also imposes a restriction on your PHP usage. For example you won't be able to use PHP code directly on any .shtml page that has an <!--#include file--> statement. A file can't be parsed as PHP and SSI at the same time.

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  4. #4
    ABW Ambassador PatrickAllmond's Avatar
    Join Date
    September 20th, 2005
    Location
    OKC
    Posts
    1,219
    I concur with Snib. So that makes it the official way to do it !

    The only thing I'd like to add to his information is that the <!-- --> is not PHP code. So I prefer the option that is all PHP as opposed to mixing the server directive and the PHP code.
    ---
    This response was masterly crafted via the fingers of Patrick Allmond who believe you should StopDoingNothing starting today.
    ---
    Focus Consulting is where I roll | Follow @patrickallmond on Twitter
    Search Engine Marketing | Search Engine Optimization | Social Media | Online Video

  5. #5
    Newbie Adrian's Avatar
    Join Date
    December 30th, 2005
    Location
    Santa Cruz, California
    Posts
    41
    Excellent responses, I will work on this.

    In a related subject then. I'm wonder what I should ideally use as an extension on my regular pages which will have various includes embedded, PHP includes and HTML includes, but also have standard html (tables, cells, images, java scripts, etc)

    I would like to use .htm but have been told the includes won't always work. That's why I now use .shtml, but others have told me they use .php .

    If I read right, the ideal solution is to name my regular pages with a .php extension so that for files in the same directory I can use:
    <?php include("file_name.php"); ?>

    If I name my regular pages .shtml, I will be limited to using the old:
    <!--#include file="file_name.php" -->

    which is SSI and "...SSI pre-dates PHP and is obsolete in some web hosts." and when used will not allow other PHP to run directly on the .shtml page just php includes.

    Therefore if I am understanding correctly. I should use .php so that I can run php as includes and directly on the page as well be sure that my page will run on more types of servers.

    I'm not sure if naming the regular pages .php will still allow all of the other functions to run the same, all the html code, java script references etc.

  6. #6
    ABW Ambassador
    Join Date
    January 18th, 2005
    Location
    Los Angeles
    Posts
    4,053
    I use PHP includes on every single site, and they're all either .htm or .html file extensions. First thing when setting up hosting, a blank robots.txt goes up to avoid 404's when crawlers hit (which can be changes later on), and this goes into .htaccess:

    AddType text/html .html .htm
    AddHandler server-parsed .html .htm

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

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example.com
    RewriteRule (.*) http://www.example.com/$1 [R=301,L]

    That'll cause the html (or htm) pages to be parsed for php, and the redirect is to avoid canonical issues (for Google).

    The code for the php may be redundant, but it works every time with no problem.

  7. #7
    Beachy Bill's Avatar
    Join Date
    November 20th, 2005
    Posts
    8,266
    Good advice and nice example, webworker. That is almost exactly what I do - and it works every time. I use .html on all site pages and even on the "included" files. I, too, would recommend staying away from using SSI
    Bill / Marketing Blog @ 12PM - Current project: Resurrecting my "baby" at South Baltimore..
    Cute Personal Checks and Business Checks
    If you are too busy to laugh you are too busy.

  8. #8
    ABW Ambassador
    Join Date
    January 18th, 2005
    Location
    Los Angeles
    Posts
    4,053
    ty, Beachy. Is that the same file-handler stuff you use in .htaccess? I've wondered if all I use is necessary or redundant.

    In a related subject then. I'm wonder what I should ideally use as an extension on my regular pages which will have various includes embedded, PHP includes and HTML includes, but also have standard html (tables, cells, images, java scripts, etc)
    There's been some confusion by one search engine out there about final forward slashes on /directories/ so just to be on the safe side, I'd use file extensions so it's perfectly clear that it's a page and not a /directory with a final forward slash missing.

    With adding the filehandler, it'll all work on .html or .htm pages - no problem. The PHP includes are processed server-side before pages are ever delivered to user agents (browsers or bots) so their use is transparent. It's just business as usual for whatever has been working for HTML coding or browser side processing.

  9. #9
    Newbie Adrian's Avatar
    Join Date
    December 30th, 2005
    Location
    Santa Cruz, California
    Posts
    41
    OK, now I'm confused.

    I tried replacing my .htaccess file as shown an the .php does not work as it does when the pages are called .shtml .

    I would really like to name my pages .htm or .html if possible for link clarity purposes.

    Also, what include code is to be used on .htm pages for php inculdes and what for htm includes?

    And to clarify I thought that php would only run if the page or the include file where it resides ends in .php.

  10. #10
    ABW Ambassador
    Join Date
    January 18th, 2005
    Location
    Los Angeles
    Posts
    4,053
    Nope, with the file-handler "instructions" in .htaccess you're telling the server to treat the .htm or .html pages just as if they were really .php pages - so using .htm or .html is fine, and the php includes will work just fine.

    You can't use php on .shtml pages - php and ssi don't play together. And with so much available in php lately for doing all kinds of things, imho it's the way to go.

    No htm includes - just php includes. You can keep all your includes in a folder called /includes/

    you would call it like this from files in subdirectories:

    <?php include ("../includes/yourfilename.php"); ?>

    And from pages in your root directory (including the homepage) you use:

    <?php include ("includes/yourfilename.php"); ?>

    I use the .php file extension simply because they're php files to be included. But the pages they are called from can be .htm or .html if you use those file handling instructions for the server in .htaccess.

  11. #11
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    Quote Originally Posted by webworker
    There's been some confusion by one search engine out there about final forward slashes on /directories/ so just to be on the safe side, I'd use file extensions so it's perfectly clear that it's a page and not a /directory with a final forward slash missing.
    That's not to say it's bad to hide your extensions. On the contrary it's much more user friendly to offer URLs without them. For example you can offer search embedded URLs like Flickr: flickr.com/photos/tags/dog where users can visit the site from a search query. And users like to have their short personalized URLs, ie. myspace.com/myname or flickr.com/photos/myname. Personally, I think it's best to mask your file types for the sake of your users. With mod_rewrite you can design a very friendly URL structure that you and your users can share by memory. For example a friend asks you where to buy some good dress shoes you can tell him to visit yoursite.com/search/dress+shoes

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  12. #12
    Newbie Adrian's Avatar
    Join Date
    December 30th, 2005
    Location
    Santa Cruz, California
    Posts
    41
    Very Interesting,

    So in this scenario where you can run php on .htm pages by adjusting the .htaccess file whereby you have '...No htm includes - just php includes."...

    How could I include html sections that I want to be shared throughout my site/sites and updated by changing just one file?

    Do I simply change the extension to .php and everything will work the same as if it were an 'htm include'???

  13. #13
    ABW Ambassador
    Join Date
    January 18th, 2005
    Location
    Los Angeles
    Posts
    4,053
    There's no such thing as an htm include. If you "call" the php include from every page on your site, then a change to just that one "included" file will cause the change to appear on all of those pages that are fetching it using that one line of code in the example above.

  14. #14
    ABW Veteran Mr. Sal's Avatar
    Join Date
    January 18th, 2005
    Posts
    6,795
    I would really like to name my pages .htm or .html if possible for link clarity purposes.
    You can name your pages .php and then use mod_rewrite to call your pages anything you want, read and get some more ideas here: mod_rewrite

    How could I include html sections that I want to be shared throughout my site/sites and updated by changing just one file?
    If you have named your pages .php then you can do something like this:

    <?php
    include('header.html');
    ?>
    <div>Hello World!</div>
    <?php
    include('footer.html');
    ?>
    ...

  15. #15
    ABW Ambassador
    Join Date
    January 18th, 2005
    Location
    Los Angeles
    Posts
    4,053
    That's not to say it's bad to hide your extensions. On the contrary it's much more user friendly to offer URLs without them.
    Not all bots can handle filenames the way they should.

    How about with no file extension at all? First time I've come across it - you can do it with that Drupal CMS when you give the pages "custom" URLs.
    GoogleGuy:
    Doesn't really matter. I lean toward .html because I'm a Unix-y guy. Windows-y folks tend to use .htm from back when Windows preferred three character extensions. I would use some extension though, Marcia. If you don't, then the spiders have to think. That violates a good rule of thumb of mine: Never make a bot have to think. They might get it wrong
    .htm/.html

    And MSN search *does* have some problems with filenames, some of them serious. Bad enough so that they've lost the homepage from some sites.

    Another issue:

    Finally got a response from godaddy.
    They allow .htaccess files, but they don't allow mod-rewriting. *sigh*
    http://www.webmasterworld.com/forum92/3183.htm
    Last edited by webworker; February 24th, 2007 at 10:25 PM.

  16. #16
    Beachy Bill's Avatar
    Join Date
    November 20th, 2005
    Posts
    8,266
    Quote Originally Posted by Marcia
    Not all bots can handle filenames the way they should...And MSN search *does* have some problems with filenames, some of them serious. Bad enough so that they've lost the homepage from some sites...
    That is why I ALWAYS use filenames ending in .html - can't afford to take any chances.
    Bill / Marketing Blog @ 12PM - Current project: Resurrecting my "baby" at South Baltimore..
    Cute Personal Checks and Business Checks
    If you are too busy to laugh you are too busy.

  17. #17
    ABW Ambassador
    Join Date
    January 18th, 2005
    Location
    Los Angeles
    Posts
    4,053
    If I had a big, big site I wouldn't hestitate to use .php file extension - if I were sure of staying on *nix/Apache. I do know that parsing html as php adds a wee bit of server processing time - but I'm not sure if that's the case with .php file extensions.

    That is why I ALWAYS use filenames ending in .html - can't afford to take any chances.
    Me too - plus, I need to keep things simple, I'm not high-tech. For small to medium sites .htm or .html is fine and I stick with them for the flexibility. And with sub-directories I always make sure to use that ending forward slash. Less confusion all around, including not having Apache figure out whether to add it on or not (like with root directory).

    It was Yahoo that had a problem with the subdirectory end/slash issue (if I remember right) - I'd never use filenames without some file extension.

    MSN's problems - like losing homepages.

    Size matters, and so does the file extension with MSN Search.
    Last edited by webworker; February 24th, 2007 at 10:48 PM.

  18. #18
    Affiliate Manager cbsturg's Avatar
    Join Date
    January 24th, 2007
    Location
    Lima OH
    Posts
    753
    It might be bad form, but I usually make up my own extension name and direct the server to parse it as php. I do this a lot when I build pages from templates and store header and body content information in different files. Thus, I usually end up having somefile.php include somefile.header and somefile.main (where the server is directed to send .header and .main files to the php module).

    It's my understanding that PHP will effectively parse any included file as if that file WERE php. I just direct my server to parse my funny extensions as php so that no one can directly access my code.
    Chris Sturgill
    "All my life I've had one dream, to achieve my many goals." - H. Simpson

  19. #19
    Action Jackson - King of the World
    Join Date
    January 18th, 2005
    Posts
    2,201
    so what to do if you want to include some php on an shtml page?

  20. #20
    ABW Ambassador
    Join Date
    January 18th, 2005
    Location
    Los Angeles
    Posts
    4,053
    If you have named your pages .php then you can do something like this:

    <?php
    include('header.html');
    ?>
    <div>Hello World!</div>
    <?php
    include('footer.html');
    ?>
    Mr. Sal, I do that on pages with .html file extension and for most all navigation elements as well, but I'd like to go further and somewhat automate the "body content" of pages also.

  21. #21
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    Quote Originally Posted by jackson992
    so what to do if you want to include some php on an shtml page?
    That's what we were saying. You can't run PHP code and SSI code in the same document.

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  22. #22
    Animal Lover
    Join Date
    January 18th, 2005
    Location
    oz
    Posts
    1,210
    I've included php in a shtml page before - you need to save the php code as a separate file and call it like you would any other html or txt file ie

    <!--#include virtual="phpfilename.php" -->

    I think webworker explained it earlier on in the thread and AdrianNetTraction has been using this method earlier on.

    cheers
    Oscar
    My DataFeed Scripts - php datafeed scripts for your site
    Shareasale datafeed scripts - to display Shareasale datafeeds
    Linkshare datafeed scripts - for multiple Linkshare merchants

  23. #23
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    Quote Originally Posted by oscar
    I've included php in a shtml page before - you need to save the php code as a separate file and call it like you would any other html or txt file ie

    <!--#include virtual="phpfilename.php" -->

    I think webworker explained it earlier on in the thread and AdrianNetTraction has been using this method earlier on.
    But you're only including the PHP document. You can't actually run PHP code in an shtml page like this: <?php if($param) { include('document.tpl'); } ?>. That's where the real problem comes in. By using SSI you are locking yourself out of all of the flexibility of PHP. There's really no reason at all to use shtml/SSI when you've got PHP.

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  24. #24
    Affiliate Manager cbsturg's Avatar
    Join Date
    January 24th, 2007
    Location
    Lima OH
    Posts
    753
    Quote Originally Posted by Snib
    But you're only including the PHP document. You can't actually run PHP code in an shtml page like this: <?php if($param) { include('document.tpl'); } ?>. That's where the real problem comes in. By using SSI you are locking yourself out of all of the flexibility of PHP. There's really no reason at all to use shtml/SSI when you've got PHP.

    - Scott
    Chris Sturgill
    "All my life I've had one dream, to achieve my many goals." - H. Simpson

  25. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. PHP include not working in WP (all of a sudden)
    By mobilebadboy in forum Blogging, Mobile and Social Media
    Replies: 4
    Last Post: August 25th, 2011, 01:01 PM
  2. PHP question - include(file.php) with variables
    By teezone in forum Programming / Datafeeds / Tools
    Replies: 8
    Last Post: September 4th, 2009, 04:54 PM
  3. PHP include bug?
    By ercole in forum GoldenCAN
    Replies: 10
    Last Post: April 27th, 2009, 03:28 PM
  4. php include with calendars script
    By jaynaud in forum Cusimano.com Scripts
    Replies: 1
    Last Post: April 8th, 2006, 12:27 PM
  5. How do I Include Ads in PHP
    By Doug247 in forum Programming / Datafeeds / Tools
    Replies: 3
    Last Post: April 20th, 2004, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •