Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Using include() in an echo statement

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Using include() in an echo statement

    Ok, i am trying to get some code to echo right, the only problem is that i have an include in the code that keeps messing up. Here is the part of the code i am having a problem with:
    PHP Code:
    echo'
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Echo CMS-- Home</title>
    <link href="main.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
    <div id="top_bar">
    <div id="top_bar_banner"></div>
        <div class="title" id="top_bar_left">
            ".include("maintitle.txt")."
        </div>

        <div class="title" id="top_bar_right">
            ".include ("sitename.txt")."
        </div>
    </div>
    </body>
    </html>
    '

    the includes output: ".include(whatever.txt")." instead of actually including the file. I have also tried ' .include(). ' but that got really messed up. Any ideas?

  2. #2
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    You can't embed include like that. It does not return the text it sends it to the output buffer(or browser depending on your server's settings). Try this
    PHP Code:
    echo
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
    <title>Echo CMS-- Home</title> 
    <link href="main.css" rel="stylesheet" type="text/css" /> 
    </head> 

    <body> 
    <div id="top_bar"> 
    <div id="top_bar_banner"></div> 
        <div class="title" id="top_bar_left"> 
            '
    ;
    include(
    "maintitle.txt");
    echo 

        </div> 

        <div class="title" id="top_bar_right"> 
            ".include ("sitename.txt")." 
        </div> 
    </div> 
    </body> 
    </html> 
    '


  3. #3
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks again man, it works great like that

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by blm126
    You can't embed include like that. It does not return the text ...
    By the way, it is possible to return a value from an include construct. See the PHP manual for more details.

    Quote Originally Posted by Titan85
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    I really don't understand the preoccupation with XHTML.

    Quote Originally Posted by blm126
    ".include ("sitename.txt")."
    You forgot to do that one, too, but I'm sure the OP gets the idea.

    Mike

  5. #5
    Join Date
    Aug 2006
    Posts
    239
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    btw, instead of include I'd recomment $myvar = file_get_contents(), include will do some nasty things if there is php code inside included file (basically the parser will invoke it before the output being sent, and not while). Besides, it's another possible security hole fixed.

  6. #6
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by ItsMeOnly
    btw, instead of include I'd recomment $myvar = file_get_contents(), include will do some nasty things if there is php code inside included file (basically the parser will invoke it before the output being sent, and not while). Besides, it's another possible security hole fixed.
    Hmm, didn't think of doing that, i'll try it and see how it works. Thanks for the info

  7. #7
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by ItsMeOnly
    btw, instead of include I'd recomment $myvar = file_get_contents(),
    Why not just use the readfile function and output the content directly?

    include will do some nasty things if there is php code inside included file
    That's not exactly nasty, is it, but expected.

    Besides, it's another possible security hole fixed.
    True, though it's only really a concern if untrusted users have access to the files, but using readfile doesn't hurt if the intention is just to output unprocessed text.

    Mike

  8. #8
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ok, i got another question about this. Say i just do an include of a page with the html rather than echo the html. I tried doing this, but then the includes on the page i include don't work. Is there any particular reason for this or is it just that you can't include a page with includes on it?

  9. #9
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    No, you can include pages with include. However, the PHP parser treats the file like HTML until you tell it to start parsing so the file must have a .php extension(technically, it just needs the correct server set-up but I'm trying to keep this simple) and the include statment must be wrapped in <?php and ?>(or equivalent, depending on your server settings)

  10. #10
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by blm126
    No, you can include pages with include. However, the PHP parser treats the file like HTML until you tell it to start parsing so the file must have a .php extension(technically, it just needs the correct server set-up but I'm trying to keep this simple) and the include statment must be wrapped in <?php and ?>(or equivalent, depending on your server settings)
    I tried including it again and it worked out just fine, i must have messed up something in the code the first time i tried to set it up. Thanks again for the help

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
  •