喜悦国际村 专业PHP开发者社区's Archiver

gaoshikao 发表于 2008-3-13 02:22 PM

静态页面生成之方案

随着网络的发展,SEO逐渐得到了我们的重视,把网站做成静态网站是势在必行了.生成静态页面的方法不外乎几种,看了很多资料,都是介绍通过OB函数控制输出的,我在这里给大家介绍我的方法: 通过file_get_contents()方法.这个方法可以获取网页的内容,包括本地的和internet上的,所以我们可以通过$content = file_get_contents("http://localhost/index.php");获取本地的或者防在服务器上的文件输出结果,然后创建指定的目录和文件,最后把内容写进去,从而实现了静态页面的输出.在这里,我发两个函数,一个是写函数,一个是读函数.完全兼容php4和php5.
function dfile_put_contents($file, &$data, $flags=0){
        // Check to see if functin exists
        if (function_exists('file_put_contents')) {
                file_put_contents($file, $data);
        }else{

                // Define constants used by function, if not defined
                if (!defined('FILE_USE_INCLUDE_PATH')) define('FILE_USE_INCLUDE_PATH', 1);
                if (!defined('FILE_APPEND'))           define('FILE_APPEND', 8);
                 
                // Varify arguments are correct types
                if (!is_string($file)) return(false);
                if (!is_string($data) && !is_array($data)) return(false);
                if (!is_int($flags)) return(false);
                 
                // Set the include path and mode for fopen
                $include = false;
                $mode    = 'wb';
                 
                // If data in array type..
                if (is_array($data)) {
                        // Make sure it's not multi-dimensional
                        reset($data);
                        while (list(, $value) = each($data)) {
                                if (is_array($value)) return(false);
                        }
                        unset($value);
                        reset($data);
                        // Join the contents
                        $data = implode('', $data);
                }
                 
                // Check for flags..
                // If include path flag givin, set include path
                if ($flags&FILE_USE_INCLUDE_PATH) $include = true;
                // If append flag givin, set append mode
                if ($flags&FILE_APPEND) $mode = 'ab';
               
                // Open the file with givin options
                if (!$handle = @fopen($file, $mode, $include)) return(false);
                // Write data to file
                if (($bytes = fwrite($handle, $data)) === false) return(false);
                // Close file
                fclose($handle);
                 
                // Return number of bytes written
                return($bytes);
        }
}

function dfile_get_contents($file, $include=false){
        // Check to see if functin exists
        if (function_exists('file_get_contents')) {
                return @file_get_contents($file);
        }else{
                // Varify arguments are correct types
                if (!is_string($file))  return(false);
                if (!is_bool($include)) return(false);
                 
                // Open the file with givin options
                if (!$handle = @fopen($file, 'rb', $include)) return(false);
                // Read data from file
                $contents = fread($handle, filesize($file));
                // Close file
                fclose($handle);
                 
                // Return contents of file
                return($contents);
        }
}

dyfire 发表于 2008-3-17 03:12 PM

路过看看~~

darkcc123 发表于 2008-3-30 10:59 PM

用ob函数会不会更简单一点呢?

xieaotian 发表于 2008-3-31 08:27 AM

力荐OB.

ydl0025 发表于 2008-4-16 12:08 PM

用了,还可以

xieaotian 发表于 2008-4-17 09:41 AM

也许OB更快些,你这个只是单纯生成了一个文件.

阿刁 发表于 2008-4-17 01:27 PM

似乎没什么技术可研.

gouki 发表于 2008-4-22 12:03 AM

其中的技术就是把file_get_contents和file_put_contents重写了一遍。
自从到了2008年。应该是全世界都用上了PHP5了吧。
所以……这两个写不写都没有什么区别了

wamper 发表于 2008-4-22 08:39 AM

链接地址修改了吗? 如果链接地址还是php的, 那这样静态化只是会提高点访问速度,对seo没什么影响

pingasi 发表于 2008-4-27 02:26 AM

楼主再继再励!!!

写个类似write的规则,然后file_put_contents时替换url,
遍历全站php生成静态,太妙了

k6bar 发表于 2008-5-4 02:52 PM

页面缓冲吧

niexa123 发表于 2010-2-19 08:22 PM

研究研究

hhsw 发表于 2010-3-19 10:32 AM

恩  用替换函数 很简单!

bigtige 发表于 2010-4-10 10:39 AM

使用一个字符串替换字符串中的另一些字符str_replace(find,replace,string,count)
mysql db table 建立 make_number 和maked_html_url 两个字段  
再来个 order by id desc 搞定 简单的很!

lzerqf 发表于 2010-4-10 08:40 PM

学习了

bigtige 发表于 2010-4-13 04:09 PM

恩 学习啦!!!!

无喱头 发表于 2010-4-16 01:51 PM

如果打开了php的安全控制呢。。。。悲剧。。。。file_get_content(ur)需要php.ini中设置fopen_url支持。。。。。

如果放到我服务器上,这样写的后果就是茶几上的器皿。。。全是怀具了。

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.