PDA

View Full Version : Using include() in an echo statement



Titan85
09-15-2006, 12:35 AM
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:
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?

blm126
09-15-2006, 12:56 AM
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


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>
';

Titan85
09-15-2006, 01:04 AM
Thanks again man, it works great like that :)

mwinter
09-15-2006, 01:04 AM
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 (http://uk.php.net/include/). See the PHP manual for more details.



<!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. :confused:



".include ("sitename.txt")."

You forgot to do that one, too, but I'm sure the OP gets the idea. :)

Mike

ItsMeOnly
09-15-2006, 10:04 AM
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.

Titan85
09-15-2006, 11:02 AM
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

mwinter
09-15-2006, 12:09 PM
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

Titan85
09-16-2006, 12:14 AM
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?

blm126
09-16-2006, 02:53 AM
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)

Titan85
09-16-2006, 03:06 AM
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

ItsMeOnly
09-16-2006, 08:46 AM
Why not just use the readfile function and output the content directly?
Because with readfile you don't have control over flow- if the file exists, it will be passed to client, if not, error will be generated- file_get_contents allows you to silently evaluate "including" (cause its return is content of the file, or FALSE), and manipulating the content received in manners of variable operations (joining, cutting, replacing, grepping, etc...).

Secondly you can focus on damage control if it's not included for some reason- think of it like "template" loading, where you can still "default" to something.


That's not exactly nasty, is it, but expected.
Well, you can trigger a php code accidently, I'd call it nasty :)

blm126
09-16-2006, 03:47 PM
<?php
@readfile('somefilethatwontbeexecuted.php') or exit('Sorry an error has occured');
?>

What is so hard about that?

mwinter
09-17-2006, 07:57 PM
<?php
@readfile('somefilethatwontbeexecuted.php') or exit('Sorry an error has occured');
?>

What is so hard about that?
Exactly. Both functions generate errors when the specified file cannot be found, those errors can be suppressed in the same way, and the error condition can be detected as both functions return boolean false.

As for manipulating the contents, the OP didn't suggest that it was necessary, so it's irrelevant. What's the point in loading data into memory only to write it out again? The readfile function should be able to efficiently send it to the output stream as that's its sole purpose.

Mike

ItsMeOnly
09-17-2006, 09:43 PM
LOL, hey- I was just saying that include should be treated with caution, and in readfile you have no control what's output., besides, with file_get_contents you can do, for example, this:


if (file_exists("$pwd/.README")) {
$myText = file_get_contents("$pwd/footer.txt");
$myText = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
"<a href=\"\\0\">\\0</a>", $myText);
$myText = nl2br($myText);
echo $myText
}

That's the code I'm using to display "README's" for dirs on my http browsable FTP directory (adds <br>'s and turns adresses into links