PHP镜像网站程序的实现原理,快速一键克隆其它网站

1、地址归一
为了获得更好的效果,所以地址我们完全仿照源站地址,这样就需要使用各种技术来接收这个地址,因为我们这里仅讨论原理,所以就使用伪静态吧,以下是apache的伪静态规则

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?s=$1 [QSA,PT,L]
</IfModule>
这个伪静态可以把所有访问不到的地址都归一到index.php入口文件上的$_GET[‘s’]

2、配置文件 config.php
我们将需要配置的单独出来,方便扩展,这里我们以chinaz为例

$url=’https://www.heiku8.com';
3、入口文件 index.php
这是核心处理部分,我们根据伪静态得到的$_GET[‘s’]参数与配置文件设置的地址组装成新地址,然后获取这个新地址的内容,当得到内容之后将页面内容的链接转换成我们自己的站内链接

include __DIR__.’/config.php';
$path=isset($_GET[‘s’])?strval($_GET[‘s’]):”;
$geturl=$url.’/’.ltrim($path,’/’);

// 文档类型
$header_arr = array_unique(get_headers($geturl));
$contentType = ”;
foreach($header_arr as $k => $v){
$header_tmp = [];
if(preg_match(“/^Content-Type:.*?$/”, $v, $header_tmp)){
$contentType = end($header_tmp);
break;
}
}
$contentType = !empty($contentType) && !preg_match(“/Content-Type:\s+text\/html/”, $contentType) ? $contentType : ‘Content-Type: text/html;charset=utf-8′;
header($contentType);

// 获取网页内容
$content=file_get_contents($geturl);
$content=str_replace($url,”,$content);
echo $content;
ok!已经完成,可以试试效果。

4、扩展子目录存放
当子目录的时候我们发现上述的地址无效了,我们增加个子目录的配置,比如我的在根目下的c子目录,在config.php增加

$sitepath=’/c';
然后修改Index.php

$content=str_replace($url,”,$content);
改为

$content=str_replace($url,$sitepath,$content);
ok,现在好了,子目录也可以使用了!

未经允许不得转载:SEO_整站优化_网站优化公司 » PHP镜像网站程序的实现原理,快速一键克隆其它网站

赞 (3)