---
title: "弄了一个中文Drupal站点大串连的缓存版"
author: "atomoptics"
date: "2006-03-01T15:44:57Z"
tags: []
slug: "drupal"
draft: false
lastmod: "2006-03-01T15:44:57Z"
id: 163069
url: "https://atomoptics.iteye.com/blog/163069"
---

非常大米搞了中文Drupal站点串联，但是要跨站调用java脚本，在上网高峰时期，DNS和大米网站的出口速度限制导致了经常无法显示。而且对于有的利用table布局的模板，会在ie下长时间无法显示整个页面（参见 http://www.kylinx.net/node/50 ）
所以，我用php做了一个带缓存功能的Drupal站点大串连，它可以自行设置每隔一段时间，才和大米的网站同步一次串连站点的数据，在你自己的服务器上实现大米串连的所有功能（当然注册站点出外），比如随机显示，这样一来速度快了，也有更多的drupaler愿意加入串连。
注意：需要你的服务器打开allow_url_fopen支持，不过好像大多都是开着的。
安装方法如下先把updateCDL.php下载到你的files目录，确保files目录可写，请新建一个Drupal中文链接的block，然后加入下面代码：Download Code: cdlBlock.php&lt;?php
$num=7; //一次显示的站点数
$cachetime=24; //从大米那更新一次的时间间隔，小时为单位
$cachefilename=&quot;files/drupal_list.html&quot;; //cache文件的文件名
$fp1=fopen($cachefilename,'r');
if($fp1===false)
{
echo&nbsp;&quot;&amp;lt;script type=\&quot;text/javascript\&quot; src=\&quot;http://www.verydummy.com/drupal_demo/drupalcnlist.php?num=7\&quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;iframe marginheight=0 frameborder=0 scrolling=no height=20 width=20 src=\&quot;files/updateCDL.php?hours=&quot;.$cachetime.&quot;\&quot;&gt;&lt;/iframe&gt;&quot;;
}
else{
$contents = fread($fp1, filesize&nbsp;($cachefilename));
fclose($fp1);
&nbsp; &nbsp; $ii=1;
&nbsp; &nbsp; &nbsp; &nbsp; while($ii)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(preg_match('#(.*?)\&lt;/li&gt;&lt;li&gt;(.*?)\&lt;/li&gt;(.*?)#sie',$contents,$match_site)){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $drupalsite =&quot;&lt;/li&gt;&lt;li&gt;$match_site&lt;/li&gt;&quot;;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $contents=strstr($contents,&quot;&lt;/li&gt;&quot;);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($contents==FALSE)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ii=0;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $contents=strstr($contents,&quot;l&quot;);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
//var_dump($drupalsite);
$rand_keys = array_rand($drupalsite, $num);
echo&nbsp;'&lt;div class=&quot;item-list&quot;&gt;&lt;ul&gt;';
while($num&gt;=0){
echo&nbsp;stripslashes($drupalsite]);
}
echo&nbsp;'&lt;/ul&gt;&lt;/div&gt;&lt;div class=&quot;more-link&quot;&gt;&lt;a href=&quot;http://www.verydummy.com/drupal_demo/node/45&quot;&gt;more&lt;/a&gt;&lt;/div&gt;';
&nbsp;
echo&nbsp;$contents.&quot;&lt;iframe marginheight=0 frameborder=0 scrolling=no height=2 width=2 src=\&quot;files/updateCDL.php?hours=&quot;.$cachetime.&quot;\&quot;&gt;&lt;/iframe&gt;&quot;;
&nbsp;
}
?&gt;
在输入格式那里选择php，然后enable它就ok了
附：updateCDL.php的代码
Download Code: updateCDL.php&lt;?php
$cachefilename=&quot;drupal_list.html&quot;; //cache文件的文件名，和block中的保持一致，
//但是前面没有files这个目录名
$listurl=&quot;http://www.verydummy.com/drupal_demo/drupalcnlist.php?num=50&quot;;
if&nbsp;(isset($_GET)) {
&nbsp; &nbsp; $updatetime=$_GET;
&nbsp; &nbsp; settype($updatetime,&quot;integer&quot;);
&nbsp; &nbsp; if&nbsp;($updatetime&lt;1)
&nbsp; &nbsp; &nbsp; &nbsp; $updatetime=12;
&nbsp; &nbsp; $updatetime=$updatetime*60*60;
&nbsp; &nbsp; //echo &quot;$updatetime&quot;;
&nbsp; &nbsp; $t=filemtime($cachefilename);
&nbsp; &nbsp; $nt=time();
&nbsp; &nbsp; if(($nt-$t)&lt;$updatetime){
&nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp;&quot;no need&quot;;
&nbsp; &nbsp; &nbsp; &nbsp; exit();
&nbsp; &nbsp; }
&nbsp; &nbsp; $fpURL=fopen($listurl,'r');
&nbsp; &nbsp; if($fpURL===false)
&nbsp; &nbsp; &nbsp; &nbsp; exit();
&nbsp; &nbsp; while(!feof($fpURL))
&nbsp; &nbsp; &nbsp; &nbsp; $contents.=fgets($fpURL);
&nbsp; &nbsp; if(strstr($contents,&quot;kylinx&quot;))
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; if&nbsp;(!$fp=fopen($cachefilename,'w'))
&nbsp; &nbsp; &nbsp; &nbsp; exit;
&nbsp; &nbsp; fwrite($fp, $contents);
&nbsp; &nbsp; fclose($fp);
&nbsp; &nbsp; &nbsp; &nbsp; echo&nbsp;&quot;ok&quot;;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; fclose($fpURL);&nbsp; &nbsp; 
}
?&gt;