Forum Moderators: coopster

Message Too Old, No Replies

Need PHP code that will load all scripts before page loads

         

kevinj

7:50 pm on Apr 27, 2005 (gmt 0)

10+ Year Member



Does anyone have any leads to PHP code that will load all scripts and everything on a page into a buffer before it displays the page? I guess this would be similar to ASP's Response.Buffer=true

I've seen the ob functions, but can't get my code to work.

Thanks!

ergophobe

8:42 pm on Apr 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The output buffering functions are it. They're pretty solid. What can't you get to work?

kevinj

8:53 pm on Apr 27, 2005 (gmt 0)

10+ Year Member



Here's my code:

<?
ob_start();
$thefile = fopen("______", "r");
readfile($thefile);
$str = ob_get_contents();
$str = nl2br(strrev($str));//do something to string
ob_end_clean();
echo $str;
?>

I can't figure out how to read the page's contents that this code is placed into. I want it to read the entire page and jscripts, and then render the page once everything is read. THe problem I'm having is that in IE 5.1 on Mac, my client's browser, the page loads partially, then refreshes and loads again with the full page. I'm not sure how to write the path to the file above that I've indicated with _______

Thanks!

coopster

2:30 am on Apr 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you tried using file_get_contents() [php.net]? You can read the entire file into a string, do what you want to it ...

ergophobe

5:03 am on Apr 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



BTW, since file_put_contents() is PHP5 only, I've started including this my function libs


if (!function_exists('file_put_contents'))
{
function file_put_contents($filename, $contents, $flags='', $resource_context='')
{
// note the last two parameters are not used, but are included just for PHP5 compatibility
if(!$handle = @fopen($filename, 'w'))
{
return false;
}
$written = @fwrite($handle, $contents);
@fclose($handle);
return $written;
}
}

kevinj

2:06 pm on Apr 28, 2005 (gmt 0)

10+ Year Member



Thanks ergophobe. I'm new to PHP, so forgive me if this is a dumb question. For the $filename can I use the page's url? Do I need to do anything with the $contents variable?

ergophobe

3:27 pm on Apr 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




$filename can I use the page's url

Maybe depending on the server setup. Safer is to use the file's location in the file system. PHP helps you out with a built-in variable that resolves the part of the file system to get you to web root, so it's as easy as using a URL (of course this only works if the file is on the server).

So if the file you want to read is in a subdir of your site so that you would get to it like this

[example.com...]

On the file system you get it like this

// locate the file
$filename = $_SERVER['DOCUMENT_ROOT'] . '/files/myfile.php';

// check that it exists
if (!file_exists($filename))
{
echo "Couldn't find the file."
exit; // if it's a function, use something like "return 0;"
}

// it does exist, so get the contents
$contents = file_get_contents($filename);

// output the contents to the screen
echo $contents;

//add a new comment to the file
$contents .= "This is my stupid comment"
$success = file_put_contents($filename, $contents);

if ($success)
{
echo "Eureka! it worked";
}
else
{
echo "Sorry, couldn't add your stupid comment".
}

All well and good, but have we actually gotten to your question?

kevinj

7:48 pm on Apr 28, 2005 (gmt 0)

10+ Year Member



You lost me. Do I need to combine the code from the two posts you gave me? Can you give me the complete code to get a page to load everything before displaying that same page? I've been trying but can't get it to work.

Thank you!

ergophobe

9:28 pm on Apr 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Do I need to combine the code from the two posts you gave me?

If you are using PHP4, yes you do.


get a page to load everything before displaying that same page?

I still don't know what this means. In general, what I do is assign everything to variables and echo via templates at the end, but in short, this will do it

ob_start();

include('everthing.php');
echo "everything in the known universe, system memory permitting."

ob_end_flush();