喜悦国际村 » 喜悦原创 » 静态页面生成之方案

页: [1]
gaoshikao2008-3-13 06:22 AM
静态页面生成之方案

随着网络的发展,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);
        }
}

dyfire2008-3-17 07:12 AM
路过看看~~

darkcc1232008-3-30 02:59 PM
用ob函数会不会更简单一点呢?

xieaotian2008-3-31 12:27 AM
力荐OB.

ydl00252008-4-16 04:08 AM
用了,还可以

xieaotian2008-4-17 01:41 AM
也许OB更快些,你这个只是单纯生成了一个文件.

阿刁2008-4-17 05:27 AM
似乎没什么技术可研.

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

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

pingasi2008-4-26 06:26 PM
楼主再继再励!!!

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

k6bar2008-5-4 06:52 AM
页面缓冲吧


查看完整版本: 静态页面生成之方案


Powered by Discuz! Archiver 6.1.0  © 2001-2006 Comsenz Inc.
Processed in 0.006596 second(s), 2 queries