如:我访问这个网页 http://fushi.38.ac.cn/2010/12/31/eda133dd-0141-4b95-9555-a19f597bc4de_1.html这个页面是通过URL重写实现的,并不存在相应的页面,实际路径是:http://fushi.38.ac.cn/article.php?url=2010/12/31/eda133dd-0141-4b95-9555-a19f597bc4de_1.php现在访问http://fushi.38.ac.cn/2010/12/31/eda133dd-0141-4b95-9555-a19f597bc4de_1.html 
过后需要自动生成 2010/12/31/三层目录和静态网页 eda133dd-0141-4b95-9555-a19f597bc4de_1.html请问article.php里的代码应该怎样写?只针对页面达到要求就可以了

解决方案 »

  1.   

    ob_start();面页代码$contents = ob_get_clean();//获得页面file_put_contents('2010/12/31/eda133dd-0141-4b95-9555-a19f597bc4de_1.html',$contents);//写到文件,路径和文件名可以从url里得到。
    echo $contents;输出页面。
      

  2.   

    ob_start();面页代码$contents = ob_get_clean();//获得页面file_put_contents('2010/12/31/eda133dd-0141-4b95-9555-a19f597bc4de_1.html',$contents);//写到文件,路径和文件名可以从url里得到。
    echo $contents;输出页面。
      

  3.   

    看看模板引擎比如 smarty  ,phplib  
      

  4.   

    如果让别人访问后,自动生成html那你最好不要再指定模版,那样属于画蛇添足。其实你把程序加到div中,形成现在的页面后,自动用ob_start将结果写入一个html文件,然后要把里面的图片和样式路径处理一下,再把这个html的地址写入数据为,下次再访问这条数据的时候就直接读html了。 //生成书下载download-txt.shtml
    $downurl="/html/exit_down.php?r=".$jmbookid;
    $downwurl="http://www.wanben.net/html/exit_down.php?r=".$jmbookid;
    $xldownurl="thunder://".base64_encode('AA' . $downwurl . 'ZZ');
    $template_file_down="../templates/downdetail.shtml";
       $handdown = fopen($template_file_down, "r");
       $dcontent  = fread ($handdown,filesize ($template_file_down));
       $dcontent = str_replace ("{bookid}",$jmbookid,$dcontent);
       $dcontent = str_replace ("{bookname}",$bookname,$dcontent);
       $dcontent = str_replace ("{bookintr}",$bookintr,$dcontent);
       $dcontent = str_replace ("{global_websitename}",$global_websitename,$dcontent);
       $dcontent = str_replace ("{bookimg}",$bookimg,$dcontent);//书图的相对路径
       $dcontent = str_replace ("{downurl}",$downurl,$dcontent);
       $dcontent = str_replace ("{xldownurl}",$xldownurl,$dcontent);
       $dcontent = str_replace ("{author}",$author,$dcontent);
       $dcontent = str_replace ("{zipsize}",$zipsize,$dcontent);
      if (create_file($downloadurl,$dcontent))这是我的站的代码你可以参考一下
      

  5.   

    每次生成前判断一下是否已经生成对应的HTML文件,没有则执行生成,若已经生成则直接返回对应的HTML文件
      

  6.   


    对,每次访问前判断一下是否已经生成对应的HTML文件,没有则执行生成,若已经生成则直接返回对应的HTML文件。
    我的内容是 $contents 要完整的生成下面页面怎么处理呢?<!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=utf-8"/>
    <meta content="<?php echo $title;>" name="Keywords"/>
    <meta content="<?php echo $title;>" name="description"/>
    <title><?php echo $title;></title>
    </head>
    <body>
    <?php echo $contents;>
    </body>
    </html>
      

  7.   

    这个是phplib摸板引擎,需要下载 template_class.phpob_start();
    require_once("template_class.php");$title = 'test_title';
    $content = 'test_this is contents';
    $p = new template( "template" );
    $p->set_file( "handle", "temp.html" );
    $p->set_var( "title", $title );
    $p->set_var( "content", $content);
    $p->parse( "output", "handle" );
    $p->p( "output" );$contents = ob_get_clean();
    $filename = 'eda133dd-0141-4b95-9555-a19f597bc4de_1.html';
    if(!file_exists($filename)){
      file_put_contents('eda133dd-0141-4b95-9555-a19f597bc4de_1.html',$contents);
    }
    echo $contents ;
    下面是模板文件 在 template目录下 temp.html<!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=utf-8"/>
    <meta content="{title}" name="Keywords"/>
    <meta content="{title}" name="description"/>
    <title>{title}</title>
    </head>
    <body>
    {content}
    </body>
    </html>
    附template_class.php<?php
    class template
    { var $classname = "Template";
    var $debug = false;
    var $file = array( );
    var $root = "";
    var $varkeys = array( );
    var $varvals = array( );
    var $unknowns = "remove";
    var $halt_on_error = "yes";
    var $last_error = ""; function template( $root = ".", $unknowns = "remove" )
    {
    $this->set_root( $root );
    $this->set_unknowns( $unknowns );
    } function set_root( $root )
    {
    if ( !is_dir( $root ) )
    {
    $this->halt( "设置目录: {$root} 不是一个目录文件." );
    return false;
    }
    $this->root = $root;
    return true;
    } function set_unknowns( $unknowns = "keep" )
    {
    $this->unknowns = $unknowns;
    } function set_file( $handle, $filename )
    {
    if ( !is_array( $handle ) )
    {
    if ( $filename == "" )
    {
    $this->halt( "设置文件句柄: 句柄->{$_obfuscate_aGFuZGxlzsS8sP7zqq1Q}." );
    return false;
    }
    $this->file[$handle] = $this->filename( $filename );
    }
    else
    {
    reset( $handle );
    while ( list( $h, $f ) = each( $handle ) )
    {
    $this->file[$h] = $this->filename( $f );
    }
    }
    } function set_block( $parent, $handle, $name = "" )
    {
    if ( !$this->loadfile( $parent ) )
    {
    $this->halt( "设置文本体: 没有加载成功{$parent}." );
    return false;
    }
    if ( $name == "" )
    {
    $name = $handle;
    }
    $str = $this->get_var( $parent );
    $reg = "/<!--\\s+BEGIN {$handle}\\s+-->(.*)\n\\s*<!--\\s+END {$handle}\\s+-->/";
    preg_match_all( $reg, $str, $m );
    $str = preg_replace( $reg, "{".$name."}", $str );
    $this->set_var( $handle, $m[1][0] );
    $this->set_var( $parent, $str );
    } function set_var( $varname, $value = "" )
    {
    if ( !is_array( $varname ) )
    {
    if ( !empty( $varname ) && $this->debug )
    {
    print "变量: 设置 *{$varname}* -> *{$value}*<br>\n";
    }
    $this->varkeys[$varname] = "/".$this->varname( $varname )."/";
    $this->varvals[$varname] = $value;
    }
    else
    {
    reset( $varname );
    while ( list( $k, $v ) = each( $varname ) )
    {
    if ( !empty( $k ) && $this->debug )
    {
    print "变量数组: 设置 *{$k}* -> *{$v}*<br>\n";
    }
    $this->varkeys[$k] = "/".$this->varname( $k )."/";
    $this->varvals[$k] = $v;
    }
    }
    } function subst( $handle )
    {
    if ( !$this->loadfile( $handle ) )
    {
    $this->halt( "获取文件: 不能载入文件 {$handle}." );
    return false;
    }
    $str = $this->get_var( $handle );
    $str = @preg_replace( $this->varkeys, $this->varvals, $str );
    return $str;
    } function psubst( $handle )
    {
    print $this->subst( $handle );
    return false;
    } function parse( $target, $handle, $append = false )
    {
    if ( !is_array( $handle ) )
    {
    $str = $this->subst( $handle );
    if ( $append )
    {
    $this->set_var( $target, $this->get_var( $target ).$str );
    }
    else
    {
    $this->set_var( $target, $str );
    }
    }
    else
    {
    reset( $handle );
    while ( list( $i, $h ) = each( $handle ) )
    {
    $str = $this->subst( $h );
    $this->set_var( $target, $str );
    }
    }
    return $str;
    } function pparse( $target, $handle, $append = false )
    {
    print $this->parse( $target, $handle, $append );
    return false;
    } function get_vars( )
    {
    reset( $this->varkeys );
    while ( list( $k, $v ) = each( $this->varkeys ) )
    {
    $result[$k] = $this->varvals[$k];
    }
    return $result;
    } function get_var( $varname )
    {
    if ( !is_array( $varname ) )
    {
    return $this->varvals[$varname];
    }
    else
    {
    reset( $varname );
    while ( list( $k, $v ) = each( $varname ) )
    {
    $result[$k] = $this->varvals[$k];
    }
    return $result;
    }
    } function get_undefined( $handle )
    {
    if ( !$this->loadfile( $handle ) )
    {
    $this->halt( "获得未定义句柄: 不能加载模板{$handle}." );
    return false;
    }
    preg_match_all( "/\\{([^}]+)\\}/", $this->get_var( $handle ), $m );
    $m = $m[1];
    if ( !is_array( $m ) )
    {
    return false;
    }
    reset( $m );
    while ( list( $k, $v ) = each( $m ) )
    {
    if ( !isset( $this->varkeys[$v] ) )
    {
    $result[$v] = $v;
    }
    }
    if ( count( $result ) )
    {
    return $result;
    }
    else
    {
    return false;
    }
    } function finish( $str )
    {
    switch ( $this->unknowns )
    {
    case "keep" :
    break;
    case "remove" :
    $str = preg_replace( "/{[^ \\t\\r\\n}]+}/", "", $str );
    break;
    case "comment" :
    $str = preg_replace( "/{([^ \\t\\r\\n}]+)}/", "<!-- Template {$handle}: Variable \\1 undefined -->", $str );
    break;
    }
    return $str;
    } function p( $varname )
    {
    print $this->finish( $this->get_var( $varname ) );
    } function get( $varname )
    {
    return $this->finish( $this->get_var( $varname ) );
    } function filename( $filename )
    {
    if ( substr( $filename, 0, 1 ) != "/" )
    {
    $filename = $this->root."/".$filename;
    }
    if ( !file_exists( $filename ) )
    {
    $this->halt( "模板: {$filename} 不存在." );
    }
    return $filename;
    } function varname( $varname )
    {
    return preg_quote( "{".$varname."}" );
    } function loadfile( $handle )
    {
    if ( isset( $this->varkeys[$handle] ) && !empty( $this->varvals[$handle] ) )
    {
    return true;
    }
    if ( !isset( $this->file[$handle] ) )
    {
    $this->halt( "载入模板: {$_obfuscate_aGFuZGxlsrvKx9K7uPbVci3tcS_5LH6}." );
    return false;
    }
    $filename = $this->file[$handle];
    $str = implode( "", @file( $filename ) );
    if ( empty( $str ) )
    {
    $this->halt( "载入模板: 句柄{$handle}, {$_obfuscate_ZmlsZW5hbWWyu7Tm1Nq78tXfyse1c7EvP4}." );
    return false;
    }
    $this->set_var( $handle, $str );
    return true;
    } function halt( $msg )
    {
    $this->last_error = $msg;
    if ( $this->halt_on_error != "no" )
    {
    $this->haltmsg( $msg );
    }
    if ( $this->halt_on_error == "yes" )
    {
    exit( "<b>程序终止.</b>" );
    }
    return false;
    } function haltmsg( $msg )
    {
    printf( "<b>模板调用错误:</b> %s<br>\n", $msg );
    }}?>
      

  8.   

    好像 ecshop 里面也是这样做的吧,不过不知道是怎么生成静态的
      

  9.   

    ob_start();面页代码$contents = ob_get_clean();//获得页面file_put_contents('2010/12/31/eda133dd-0141-4b95-9555-a19f597bc4de_1.html',$contents);//写到文件,路径和文件名可以从url里得到。
    echo $contents;输出页面。
      

  10.   


    在服务器上使用 出现这样
    模板调用错误: 设置目录: template 不是一个目录文件.
    程序终止.
    本地调试正常,别的服务器也能行 是怎么回事?