Closed Thread Icon

Topic awaiting preservation: Capturing an include() in PHP (Page 1 of 1) Pages that link to <a href="" title="Pages that link to Topic awaiting preservation: Capturing an include() in PHP (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Capturing an include() in PHP <span class="small">(Page 1 of 1)</span>\

Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 01-08-2002 17:51

Say I want to build a $page_display like so:

$page_display = include("includes/headers/");
$page_display .= "</HEAD>\n\n<BODY bgcolor=#cc6600 background=images/adminbg.gif>\n\n";
$page_display .= "<table cellpadding=10 cellspacing=0 border=0 width=100% height=100%>\n<tr>\n";


and echo it to the browser later, how do I do this? The included file displays to the browser as sonn as it is called in the code. How do I stop it from outputting before I'm ready?



Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 01-08-2002 18:31

well, either by calling the include when you need it, or by using php's outputbuffering functionallity. for a start, take the manual and look into ob_start().

Oh, and you're aware that PHP drops out of PHP mode when it opens an include? IE if you have phpscript in the include, the include must look like <?php your code here ?>...

hope this helps,

Tyberius Prime

Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 01-08-2002 19:11

Butcher, I would suggest you to read include() page from PHP manual thoroughly to familiarize yourself how includes work and how you can return some value from included file...

Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 01-08-2002 23:12

Thanks TP, I was reading the manual and I could probably use that, I'll have to do some more reading to put it to use.


I actually did read that page of the manual before I posted the question, but the whole return paragraph didn't really sink in. I wasn't thinking of returning a value from an include like you would from a function.

Thanks for sending me back there. I knew if you sent me back to a particular spot, I must have missed something. I got it goin on now!


Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 01-09-2002 06:12

Butcher, if your include file has just plain text and no PHP code at all, then use something like:
$page_display = join('', file('includes/'));

btw, I prefer that you don't use .INC extention for files that have PHP code in them...cause Web Server will not pass them to PHP unless you have done some extra configuration, so anyone can download the source code of your .inc files ...therefore name your files as: includes/ , or file_to_include_inc.php

Paranoid (IV) Inmate

Insane since: Apr 2000

posted posted 01-09-2002 06:45

Just watch how far you nest includes. An include within an include isn't a good idea.

Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 01-09-2002 09:17

Yeah, the .inc thing is bad. However I still can't seem to kick the habbit so I had my sys admin add the .inc extention so apache would phrase it as a php script.

Another thing I always make a habbit of is to put all of my sensitive files outside the server's root directory. I recommend this as it's very simple to do but you'd be supprised how many people just don't do it.

Oh, another thing, if your going to be using include() every time the script runs you might be better off using require() as it's a tiny bit faster than include(). But only if the require() file will be used each and every time the script is processed.

« BackwardsOnwards »

Show Forum Drop Down Menu