SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Include file within an Include file?

    I have a php script which has a link to a TPL file which has a link to an INC file. I don't think the extension should matter, so my question is - can an include file also have an include file?

    Hope that makes sense!

  2. #2
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I should probably keep out of this because I'm guessing, but I've never been shy of passing on my ill-formed opinions in the past

    Assuming that it works in the same way as when you have an include file in an HTML file, the extension of the file (usually .shtml) is what tells the server that it needs to interpret the file and find out what needs including and presumably this only happens once at the point where the server serves the page.

    If you tried to include a .shtml file in another .shtml file, then I doubt it would work as the first include is simply being included and not interpreted.


    You could just try it

    Andy

  3. #3
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think you have to use the include_once() command. This should also be in the PHP forum, since you're using a PHP script
    Mike
    It's not who I am underneath, but what I do that defines me.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I have tried it and it didn't work, that's why I was wondering if it's even possible.

    The structure of the files is like this:

    main file-->main include file-->sub include file

    The "sub include file" is an include file of "main include file" (just to make sure it's not confused with being a second include file within "main file")

    The "main include file" shows the content as expected, but the "sub include file" is ignored.

    Yes, the first file is a php file, but I didn't think it was relevant enough to post in the php forums (since I am simply including files via html tags eg, <!--#include file="menu.inc"-->). That's why I'm not sure what include_once() is going to do for me ...

  5. #5
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I'm pretty sure its impossible to use the shtml type includes in a php file. Why can't you just use the php include method?

    PHP Code:
    <?PHP

         
    include('file1.inc');

    ?>
    Mike
    It's not who I am underneath, but what I do that defines me.

  6. #6
    SitePoint Enthusiast plattopus's Avatar
    Join Date
    Aug 2002
    Location
    Melbourne, Australia
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To use your example, let's say index.php contains the following:

    PHP Code:
    <?
    include ("template.tmpl");

    echo 
    'This is some content! Hooray!";
    ?>
    And template.tmpl contains this:

    PHP Code:
    <?
    include ("include.inc");

    echo 
    'This comes from the template.tmpl file!";
    ?>
    And last but not least, your include file:

    PHP Code:
    <?
    echo 'This was included from include.inc';
    ?>
    Now, when you call index.php in your browser, this is what the server will process:

    PHP Code:
    <?
    echo 'This was included from include.inc';
    echo 
    'This comes from the template.tmpl file!";
    echo '
    This is some contentHooray!";
    ?>
    So it works in reverse order, which makes sense if you replace each include() with the contents of the file being included.

    Now, to your error: is your webserver set up to parse .tpl and .inc files as PHP? If not, you'll have to change your configuration, because it doesn't know that you want to process your files as PHP... it doesn't recognize the filetype, so will probably output the PHP in plain text.

  7. #7
    Web Genius
    Join Date
    Nov 2001
    Location
    Canada
    Posts
    708
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it's good practise to not use the .inc file extension as most servers aren't configured for them and just parse them as normal text files. Which means anyone who knows the path to the .inc file can view your raw source - perhaps database connection usernames and passwords.

    Always make include files .php themsevles

  8. #8
    SitePoint Enthusiast plattopus's Avatar
    Join Date
    Aug 2002
    Location
    Melbourne, Australia
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Agreed... or if you really need to have a different filename, use filename.inc.php or filename.tpl.php or whatever... it'll work the same but you can identify the difference quickly.

  9. #9
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by ramprage
    I think it's good practise to not use the .inc file extension as most servers aren't configured for them and just parse them as normal text files. Which means anyone who knows the path to the .inc file can view your raw source
    In most cases yes, but anyone who uses .inc should be smart enough to have this in their .htaccess file:

    <Files ~ ".inc">
    Order allow,deny
    Deny from all
    </Files>

    That prevents any browser from viewing the .inc file/ You can also have .htaccess prevent any site but your include the .inc files, but I don't know that code off hand
    Mike
    It's not who I am underneath, but what I do that defines me.

  10. #10
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What?!

    Extension of files you are including in PHP doesn't matter at all! Infact, I don't even have to give a file an extension. Only times extensions come into play is when a client is requesting a file and that is when your file extensions matter, so that the webserver knows where to dish the processing of files off to.



    Also a small note naramation, that info that you put in an .htaccess file will also block files that have ".inc" anywhere in their file name.
    Oh no! the coots are eating my nodes!

  11. #11
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well I owuldn't have .inc in anything else then
    Mike
    It's not who I am underneath, but what I do that defines me.

  12. #12
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another thing to also remember, if you're in a remembering mood, is that nested include's are always relative to the file originally including the first include.
    Oh no! the coots are eating my nodes!

  13. #13
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by naramation
    Well I owuldn't have .inc in anything else then
    I found it out the hard way when I was testing some classes and they started not showing up in directory listings. After a day or two I remembered I had an experimental .htaccess setup somewhere near the root dir which had that in it. My classes had the file name structure: class.ClassName.inc.php

    Doh!
    Oh no! the coots are eating my nodes!

  14. #14
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My problem isn't with PHP ... the include file that is not showing any content is a simple text file that shows menu options. If I include this file within the main page (whether the main page is php, asp, or html), it works fine. If I include this file within an include file of the main page, I see nothing. I'm just wondering if multiple levels of include files are permitted in general.

  15. #15
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you're talking about server side includes then?

    In that case I can't help you one bit.
    Oh no! the coots are eating my nodes!

  16. #16
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, in general, they do work.
    Mike
    It's not who I am underneath, but what I do that defines me.


Bookmarks

Posting Permissions

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