Re: Re: Writing static file from dynamic PHP page

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 18 Jan 2005 04:05:27 -0000, Matthew Weier O'Phinney
<matthew@xxxxxxxxxx> wrote:
> * Chris Bruce <chris@xxxxxxxxxxxx>:
> > I am looking for a way to write to a file what the browser would see
> > (raw html) when viewing a dynamic PHP page. I have numerous include
> > files, MySQL db queries, loops etc. and I want to generate the static
> > result of that file and save it as an html page. I have toyed a little
> > with output buffering to no avail.
> 
> The key to doing this sort of thing is to *NOT* echo everything as you
> receive it. Store content in variables and echo once you've finished.
> 
> If this is not an option, I've seen any number of good threads on using
> output buffering in recent weeks on this list; peruse them, see what has
> worked for others, and try those methods; if they don't work, ask the
> list for help -- and include that information (what you tried, what you
> expected, what you received).
> 
> If it *is* an option, Cache_Lite from PEAR is an excellent class for
> doing exactly what you're trying to do.
> 
I did some 'playing' in this area a little while ago, with some
success, but did not complete it due to some other technical
difficulties that I did not have time to resolve. Anyhow, I was saving
the buffered output as an MD5'd name including session variables and
the requested URL string to ensure that the effect of session
variables was taken into consideration. The code may help.

The code was:  

(please excuse any poor coding, I've only been doing PHP a year)

<?php
  // Class to handle hashing and file checking/retrieval
  include("includes/staticHtml.inc.php");
  
  if ($_SERVER['QUERY_STRING'])
      $to_hash = $_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING'];
  else
      $to_hash = $_SERVER['PHP_SELF'];

  $static = new static_html($to_hash, $_SESSION);
  if ($static->status == 'new') // Did not exist
  {
  	ob_start();  // Start output buffering

  }else{
  	echo $static->html;
	exit();
  }

// Page processing here

$str = ob_get_contents();
//Save static html
$static->save_html($str);
ob_flush();

?>

**** includes/staticHtml.inc.php ****
<?php
class static_html 
{
    var $status;
    var $html;
    var $hash;
    var $static_path;

    function static_html($fullUrl, $session) 
    { // constructor
        $this->$static_path = "/static";
        $ssn = implode('_',$session);
        $this->hash = md5($fullUrl.$ssn);;
        $this->find_html();
    }
	
    function find_html() 
    {
        $path = $this->$static_path.$this->hash.'.htm';
        if (file_exists($path))
        {
            $this->html = file_get_contents($path);
            $this->status = 'old';
        }else{
            $this->status = 'new';
        }
    }
	
    function save_html($str) 
    {
        if ($this->find_html() != 'old')
        {
            // save html
            $path = $this->$static_path.$this->hash.'.htm';
            $fp = fopen($path,'w');
            $sts = fwrite($fp,$str);
            fclose($fp);
        }
    }
}
?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux