Forum Moderators: coopster

Message Too Old, No Replies

Grab output html

         

dlab

3:52 pm on Dec 21, 2003 (gmt 0)

10+ Year Member



I was wondering if I included something like "includes/encodepage.php" into mainfile.php ( Which gets included by almost every php file on my site ) If you could grab the output html into a variable so I can process it before it gets sent to the clients browser.

I want to make a script that encodes the pages with javascript before it gets sent to the client, but I need to figure this out first..

Can anyone help me?

NickCoons

8:42 pm on Dec 21, 2003 (gmt 0)

10+ Year Member



You can probably accomplish what you want, but not with the method that you've mentioned. Off the top of my head, I'm not sure of the best way to do this, but one possibility would be to use $HTML = fopen("http://...") to open the page locally on your site. This way the variable $HTML now includes all of the HTML that would normally be put through to the browser.

That is probably not the best way, or even a good way to do it. There are all sorts of implications involved with that: it's going to double-up the traffic on your site, it's going to take longer for your server to respond, and trying to organize the URLs could be a pain.

This sounds like a buffering issue. I would check some of the buffering functions on PHP's site to see if there's a way you can redirect the output elsewhere so you can perform further processing on it.

phpologist

11:46 pm on Dec 21, 2003 (gmt 0)

10+ Year Member



I'm not sure if this is what you're looking for but the output buffering functions should help :

// send all output to the buffer instead of the browser
ob_start();

// include the file, any output from it is stored in the buffer
include 'foobar.php';

// ob_get_clean returns the contens of the buffer, then clears the buffer and switches off output buffering
// so any output beyond this point goes to the browser again and not the buffer
$html = ob_get_clean();

// $html now contains whatever was output by foobar.php

daisho

2:30 pm on Dec 22, 2003 (gmt 0)

10+ Year Member



I use "ob_start" but register it with a custom handle.

ie ob_start( "myhandler" )

myhandler is a function defined as such:

function myhandler( $bufferin ) {
return $bufferin
}

$bufferin is a string containing whatever was buffered and you return whatever you want to be outputted.

This way you need not put anything at the end of your scripts. At the end of the script the buffer is automatically flushed (or the client woulndn't get any output). When that happens your function is called and it's passed a string containing everything that will be sent to the client. You can at this point change it if you wish.

It's important to note that ob_start() can be layered which is very handy if you want multiple independant handlers to run. They are run in reverse order to which you issued the ob_start command.

ie

ob_start( "handler_1" );
ob_start( "handler_2" );
ob_start( "handler_3" );

at the end of the output handler_3 (being passed whatever the script intends to output) would run then handler_2 would run (but be passed the return value of handler_3) then handler_1 would run (being passed the output of handler_2)

daisho.