Apr 18, 2007

搜索机器人访问控制

关键词:搜索 机器人 Web Spider php

主页在载入速度对浏览者来说是一个比较关键的因素。一般来说,浏览者的容忍极限为5s左右,如果读取时间大于5s,浏览者往往会选择放弃。因此,我采用了现在的Live Archive方式,用 http://blog.znsunimage.com/archives.html 来代替我现在的主页,同时对该页面内的内容进行精简,将内容控制在80k以内,以提高那些新访问者的速度。

但是,相应的问题也来了。Live Archive的结构不利于搜索机器人(Web Spider)爬行,且它调用了不少的javascript内容,搜索机器人(Web Spider)无法跟踪;其次,正常主页上的诸多好友链接也被“精简”,影响了交互。虽然部分问题可以用设定robots.txt的方式来弥补,但是还是有些欠缺。

因此做了如下的处理:

  1. 让用户可以自己设定浏览方式。
    用户可以通过点击左侧“我的网站”中的Live Archive/Normal Mode 来切换浏览主页的方式,同时结果保存在浏览器Cookies中,下次用同一台电脑访问时候,系统会自动根据用户喜好选择不同的浏览方式。比方说我自己就采用正常浏览方式,因为大量的内容已经被缓存在我的浏览器中,对我来说速度已经不是瓶颈问题。
  2. 将搜索机器人(Web Spider)直接引导到正常的主页,使得他们能够收集更详细全面的信息,包括我的诸多好友链接。主要参考了该网页,精简为下面的一段代码:

    $UA=$_SERVER['HTTP_USER_AGENT'];
    if eregi("Googlebot|Yahoo|VoilaBot|Ask Jeeves|SpeedySpider|MSNbot|Yahoobot|Baiduspider|Sohubot|Lycos|Robozilla|Inktomi Slurp|larbin|spider|crawlGoogle AdSense|EchO!|Magpie|InternetSeer|Magpie|Alexa|The World Wide Web Worm|Voyager|robot|Speedy Spider|Motor|AskJeeves|GigaBot|The Python Robot|MSIECrawler|GetBot|SurveyBot|Fish search|Netcraft",$UA) //大小写没有影响
    {$file = "/home/znsunima/public_html/wordpress/index2.html";
    ob_start('ob_gzhandler');
    echo implode(", file($file));
    ob_end_flush();}
    else
    header("Location: http://blog.znsunimage.com/archives.html"); //见注¹

上面代码的原理是利用HTTP_USER_AGENT参数来判断是否为搜索机器人²,如果是,返回压缩过的正常主页;反之,返回Live Archive版本的主页。如果你用的是firefox,可以通过在地址栏输入“about:config”,新增general.useragent.override 字段后设定User_Agent来测试。

注¹. 其实其中的archives.html其实我也用php压缩过了,因为我的服务器不支持html格式的直接压缩
注². 网上的机器人很多,我只列了访问过我网站的和其他一些常见的Spider。

  
Post by SUN @ 12:07 pm 与时俱进, 网站更新 | trackback
Apr 6, 2007

优化WP-Cache:Wordperss优化手册(2)

关键词:WP-CacheWordpress优化加速Gzip
注:
♣ 本文是加速Wordpress-终极优化手册一文的补充,强烈建议您先参考终极优化手册后再看此文。
♣ 本文的方法只适用于单作者(不开放注册)的wordpress系统(但我相信目前绝大多数wordpress系统都是不开放注册的)。

加载WP-Cache插件是wordpress中最为常用的一种优化方式,同时,php程序本身也提供了一种非常强大的优化方式,也就是Gzip压缩传输。非常遗憾的是,WP-Cache插件要求关闭wordpress的内置gzip压缩功能。因此,我们要实现的就是在WP-Cache中使用Gzip的功能。这一思路的实现,我在终极优化手册中已经提到:

打开wp-cache-phase1.php,大概在35行左右找到如下代码:

foreach ($meta->headers as $header) {
header($header);

在此代码前添加下面的代码:

if ( extension_loaded('zlib') ) ob_start('ob_gzhandler');

OK,现在问题来了。
让我们深入分析一下加了Gzip功能的WP-Cache的工作原理:
首先,WP-Cache在用户第一次访问页面的时候,将生成的页面传输给用户,同时在服务器上缓存了页面,这第一次传输给用户的页面是没有用gzip方式压缩的。
第二,我们看wp-cache-phase1.php中的最后一个函数function wp_cache_get_cookies_values(),不难发现wp-cache是根据访问者的email来判断是否为同一用户的。问题就在WP-Cache的的这个判断机制上:

WP-Cache为不同的用户生成了不同的缓存页面。而这些用户第一次访问某一页面时候,WP-Cache发送的都是未经过Gzip压缩的页面。因此,除非这些用户再次访问该页面,否则他们都是享受不到Gzip压缩带来的好处的。

既然知道了问题的症结所在,解决的方法也就显而易见了。我们可以换用判断用户网址的办法来判断是否为同一用户,因为大多数用户都不会填写网址。这样用户A访问后,其他的BCDE等用户来访问,系统一般都会将用Gzip压缩过的页面传输给他,大大提高了缓存页面的利用率。但是这会产生一个新的问题:当用户A访问某一页面后,缓存页面中也就保留了他填写的用户名和email地址(comments.php中用php来读取缓存)。这个问题很好解决,我们可以用javascript来读取缓存

现在要做的就是修改代码了:
在wp-cache-phase1.php文件中找到如下代码

if (preg_match("/^wordpress|^comment_author_email_/", $key)) {

在前面加上//将其comment out,随后在该行的下一行添加如下代码:

if (preg_match("/^wordpress|^comment_author_url_/", $key)) {

接着,打开你的theme中的comments.php文件,找到填写comment的表单部分,这里以theme/default下的文件为例:

<input type="text" name="author" id="author" value="<?php echo $comment_author; ?>"

将其中的 value="<?php echo $comment_author; ?>"删除。并以此类推,删除表单中email和url的value项。
随后,我们在紧跟表单后的<?php endif; ?>之前添加如下的代码,用以读取用户的Cookies:

<script type="text/javascript">
//<![CDATA[
// Set cookies
var aCookie = document.cookie.split("; ");
function GetCookie(sName, id)
{
// cookies are separated by semicolons
var something = document.getElementById(id);
// set value to NULL for new visitors
something.value="";
if(!something) return 0;
for (var i=0; i < aCookie.length; i++)
{
// a name alue pair (a crumb) is separated by an equal sign
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
{
//reading cookie value
something.value=unescape(aCrumb[1]);
return 1;
}
}
// a cookie with the requested name does not exist
return 0;
}
GetCookie("comment_author_8e11b42cc2f3a74aac664cc9afa5baf7", "author");
GetCookie("comment_author_email_8e11b42cc2f3a74aac664cc9afa5baf7", "email");
GetCookie("comment_author_url_8e11b42cc2f3a74aac664cc9afa5baf7", "url");
//]]>
</script>

请注意,其中的"comment_author_"中的长串字符为系统生成,请自行查找后替换。(我是用firefox的web developer插件看cookie的名字的)OK,把所有修改过的文件保存一下,上传,试试下效果吧。

Post by SUN @ 10:53 pm 与时俱进 | trackback
Mar 19, 2007

WP Real Static Homepage

PLEASE Scroll down for Chinese version

This plugin enables the wp system to convert your dynamic homepage to static index.html, more importantly, keep the content up to date. It can speed up the loading for your homepage.

Readme:

  1. Plugin WP-Cron is needed for this plugin, I've packed WP-Cron v 1.4 in the zip file.
    Please upzip all the files to plugins/static-index, so that it look like:
    ------plugins/
    ------------static-index/
    ----------------static.php
    ----------------wp-cron-static-index.php
    ----------------wp-cron.php
  2. Active two plugins :WP-cron and WP-Cron Static Homepage.
  3. Create a file named index.html in your blog root folder, set
    666 permissions for it.

Well, the system will update index.html automatically (per 15 mins), you may hack wp-cron.php at line 49 to change the time for scheduled exection actions.

More Tips:

  1. You may use the following php to refresh homepage when comments come in.

    <?php
    include (get_settings('siteurl') . "/wp-content/plugins/static-index/static.php");
    ?>

  2. The following script is to demo how to set cookies in the html files.

    <script type="text/javascript">
    function GetCookie(sName, id)
    {
    // cookies are separated by semicolons
    var something = document.getElementById(id);
    if(!something) return 0;
    for (var i=0; i < aCookie.length; i++)
    {
    // a name alue pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0])
    {
    if(sName=="comment_author_10add94cfc29a64b08306c8a8f9edb30")
    something.value = decodeURI(aCrumb[1]);
    else
    something.value=unescape(aCrumb[1]);
    return 1;
    }
    }
    // a cookie with the requested name does not exist
    return 0;
    }
    //please fill in with your own cookie id
    GetCookie("comment_author_8e11b42cc2f3a74aac664cc9afa5baf7", "authorname");
    GetCookie("comment_author_email_8e11b42cc2f3a74aac664cc9afa5baf7", "email");
    GetCookie("comment_author_url_8e11b42cc2f3a74aac664cc9afa5baf7", "url");</script>

Show More >
Post by SUN @ 7:32 am 我的页面 | trackback
Mar 16, 2007

WP Static Homepage

PLEASE Scroll down for Chinese version

This plugin enables the wp system to convert your dynamic homepage to static index.html, more importantly, keep the content up to date. It can speed up the loading for your homepage.

Readme:

  1. Plugin WP-Cron is needed for this plugin, I've packed WP-Cron v 1.4 in the zip file.
    Please upzip all the files to plugins/static-index, so that it look like:
    ------plugins/
    ------------static-index/
    ----------------static.php
    ----------------wp-cron-static-index.php
    ----------------wp-cron.php
  2. Active two plugins :WP-cron and WP-Cron Static Homepage.
  3. Create a file named index.html in your blog root folder, set
    666 permissions for it.

Well, the system will update index.html automatically (per 15 mins), you may hack wp-cron.php at line 49 to change the time for scheduled exection actions.

More Tips:

  1. You may use the following php to refresh homepage when comments come in.

    <?php
    include (get_settings('siteurl') . "/wp-content/plugins/static-index/static.php");
    ?>

  2. The following script is to demo how to set cookies in the html files.

    <script type="text/javascript">
    function GetCookie(sName, id)
    {
    // cookies are separated by semicolons
    var something = document.getElementById(id);
    if(!something) return 0;
    for (var i=0; i < aCookie.length; i++)
    {
    // a name alue pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0])
    {
    if(sName=="comment_author_10add94cfc29a64b08306c8a8f9edb30")
    something.value = decodeURI(aCrumb[1]);
    else
    something.value=unescape(aCrumb[1]);
    return 1;
    }
    }
    // a cookie with the requested name does not exist
    return 0;
    }
    //please fill in with your own cookie id
    GetCookie("comment_author_8e11b42cc2f3a74aac664cc9afa5baf7", "authorname");
    GetCookie("comment_author_email_8e11b42cc2f3a74aac664cc9afa5baf7", "email");
    GetCookie("comment_author_url_8e11b42cc2f3a74aac664cc9afa5baf7", "url");</script>

Show More >
Post by SUN @ 5:22 pm 与时俱进 | trackback
Mar 10, 2007

优化Wordpress-wp终极优化手册

更新:关于wp-cache的优化,请参见本站新作:
优化WP-Cache:Wordprss优化手册(2)

Wordpress的功能非常强大,诸多的插件、花哨的界面,使得它的读取速度也大大降低。因此,如何优化以加快访问者的浏览速度便成了wper们思考的问题。我将诸多高手的方法整理总结了一下,汇成此文,希望能对大家有所帮助。 (建议将WP系统更新至2.1.2 以上)

  1. 优化所有的图片(images):
    图片其实是影响网页读取速度的关键,稍大一些的图片就可能达到100K;因此,在上传自己的图片之前,务必用Photoshop的"保存至网页"(save for web)功能,对图片进行优化。(例如,保存成GIF, PNG格式等等)
  2. 关闭一切不必要的插件(plugins):
    在安装插件之前,首先问一下自己,你是否必须要此插件。不要安装UTW之类的超大插件,尽可能的降低插件数量。
  3. 开启WP自带的缓存(Cache)功能:
    1. 打开WP根目录下的wp-config.php文件,在最后一行之前添加 define('ENABLE_CACHE',true);
    2. 将WP-content目录下的cache目录属性改为777,以保证系统能够写入cache。
    3. 如果您的blog日访问量在200以上,建议安装WP-Cache插件,否则不推荐使用。
    4. 让WP-Cache也用上Gzip:如果你安装了WP-Cache,一定知道WP-Cache要求必须关闭WP的内置GZip功能,但是我们可以在wp-cache-phase1.php中添加代码来实现Gzip压缩功能。
      打开wp-cache-phase1.php,大概在35行左右找到如下代码:

      foreach ($meta->headers as $header) {
      header($header);

      在此代码前添加下面的代码:

      if ( extension_loaded('zlib') ) ob_start('ob_gzhandler');

(more...)

Post by SUN @ 10:35 pm 与时俱进 | trackback