寂静小站

Ruby on Rails初学小结

前段时间研究PHP Framework,总是很疑惑:为何这些framework都说自己是模仿Ruby on Rails?ROR(Ruby on Rails)有多大的魅力?于是最近折腾了一番,总结了下ROR入门知识:

  1. 安装:Instant Rails(解压直接可用)
  2. 总是提示libmysql.dll有问题?把这个文件从mysql/bin中复制一份到ruby/bin中即可
  3. Rails的版本并不先后兼容,特别是2.0版,不少Rails相关的书籍都是在1.X版期间写的
  4. 推荐DOC站点:http://railsbrain.com

将Wordpress迁移到Wordpress mu时URL转向的一些经验

为了方便开CMS(不能单纯的说是博客了),今天把寂静小站的程序从Wordpress改为了Wordpress mu。

这年代链接很值钱,所以为了避免原先的链接出现404错误,对URL进行了重定向。

由于我对Apache不熟悉,所以采用的是PHP正则匹配来实现重定向,方法就是把重定向代码添加到index.php的顶部,具体代码如下:

if( $_SERVER['REQUEST_URI'] != '/'
&& $_SERVER['REQUEST_URI'] != '/feed/'
&& $_SERVER['REQUEST_URI'] != '/comments/feed/'
&& $_SERVER['REQUEST_URI'] != '/sitemap.xml'
&& $_SERVER['REQUEST_URI'] != '/sitemap.xml.gz'
&& $_SERVER['REQUEST_URI'] != '/about/'
&& $_SERVER['REQUEST_URI'] != '/robots.txt'
)
{
if( !preg_match('/^\/blog\//', $_SERVER['REQUEST_URI'])
&& !preg_match('/^\/blog\/tag\//', $_SERVER['REQUEST_URI'])
&& !preg_match('/^\/\?/', $_SERVER['REQUEST_URI'])
&& !preg_match('/^\/page/', $_SERVER['REQUEST_URI'])
)
{
Header("HTTP/1.1 301 Moved Permanently");

Header("Location:http://".$_SERVER['HTTP_HOST']
.rtrim(dirname($_SERVER['PHP_SELF']), '/\\')
."/blog".$_SERVER['REQUEST_URI']);

exit;
}
}

可能还有一些疏漏我没有发现,欢迎各位指出,谢谢!

Internet of Things(物联网) – Surprising, Enjoying & More

Surprising: SixthSense ↓

Enjoying: 微软Office实验室-愿景未来2019 ↓

More: Kevin Kelly: Predicting the next 5,000 days of the web ↓

有中文字幕的版本: http://www.ted.com/index.php/talks/kevin_kelly_on_the_next_5_000_days_of_the_web.html
Wikipedia: http://en.wikipedia.org/wiki/Internet_of_Things

网站服务器近期将进行大幅度改动

网站服务器近期将进行大幅度改动,可能造成网站临时无法访问或访问速度缓慢,敬请谅解:)

Codeigniter支持UTF8(中文/日文等)URI的简单办法

目前我使用的是Codeigniter 1.7.2版,其URI路由不支持中文等UTF8编码,原因是其直接把URI进行正则匹配,而没有先进行URL编码。

解决方法很简单,就是URI在正则匹配之前进行URL编码即可。

有两种方法,一种是直接修改Codeigniter的libraries的URI.php中的:(约在191行)

if ( ! preg_match("|^[".str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-'))."]+$|i", $str))

改为:

if ( ! preg_match("|^[".str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-'))."]+$|i", urlencode($str)))

另外一种方法就是扩张URI类,防止今后升级Codeigniter时被覆盖代码,具体方法如下:

在application/libraries/下新建MY_URI.php,内容只需复制_filter_uri($str)函数即可,并在复制后把

if ( ! preg_match("|^[".str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-'))."]+$|i", $str))

改为:

if ( ! preg_match("|^[".str_replace(array('\\-', '\-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-'))."]+$|i", urlencode($str)))

让Codeigniter缓存CSS和JS等类型的文件

虽然Codeigniter可以通过
$this->output->set_header();
来设置文件类型,但当使用Cache功能来缓存非text/html类型的文件时,输出时依然是text/html

要让缓存文件也可以输出成css、js等非text/html类型文件,只需添加一个增强类库即可,方法如下:

  1. 在application/libraries文件夹下,新建一个文件名为MY_Output.php的文件(注意文件名的大小写)文件内容见下文
  2. 在使用缓存指令$this->output->cache();之前引用该类库:$this->load->library('MY_Output');$this->output->cache();

如此便可缓存并输出css和js类型的文件了。

MY_Output.php内容:

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* MY_Output
*
* Modified _write_cache and _display_cache to allow for header caching
* works with CodeIgniter 1.7.0
*
* @package        CodeIgniter
* @subpackage    Libraries
* @category    Output
* @author        Arjen van Bochoven
* @link        http://codeigniter.com/wiki/Cache_headers
*/

class MY_Output extends CI_Output {

// ——————————————————————–

/**
* Write a Cache File
*
* @access    public
* @return    void
*/
function _write_cache($output)
{
$CI =& get_instance();
$path = $CI->config->item(‘cache_path’);

$cache_path = ($path == ”) ? BASEPATH.’cache/’ : $path;

if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
{
return;
}

$uri =    $CI->config->item(‘base_url’).
$CI->config->item(‘index_page’).
$CI->uri->uri_string();

$cache_path .= md5($uri);

if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
{
log_message(‘error’, “Unable to write cache file: “.$cache_path);
return;
}

$expire = time() + ($this->cache_expiration * 60);

if (flock($fp, LOCK_EX))
{
fwrite($fp, serialize(array(‘exp’ => $expire, ‘headers’ => $this->headers, ‘output’ => $output)));
flock($fp, LOCK_UN);
}
else
{
log_message(‘error’, “Unable to secure a file lock for file at: “.$cache_path);
return;
}
fclose($fp);
@chmod($cache_path, DIR_WRITE_MODE);

log_message(‘debug’, “Cache file written: “.$cache_path);
}

// ——————————————————————–

/**
* Update/serve a cached file
*
* @access    public
* @return    void
*/
function _display_cache(&$CFG, &$URI)
{
$cache_path = ($CFG->item(‘cache_path’) == ”) ? BASEPATH.’cache/’ : $CFG->item(‘cache_path’);

if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
{
return FALSE;
}

// Build the file path.  The file name is an MD5 hash of the full URI
$uri =    $CFG->item(‘base_url’).
$CFG->item(‘index_page’).
$URI->uri_string;

$filepath = $cache_path.md5($uri);

if ( ! @file_exists($filepath))
{
return FALSE;
}

if ( ! $fp = @fopen($filepath, FOPEN_READ))
{
return FALSE;
}

flock($fp, LOCK_SH);

$cache = ”;
if (filesize($filepath) > 0)
{
$cache = fread($fp, filesize($filepath));
}

flock($fp, LOCK_UN);
fclose($fp);

// Restore the cache array
$cache = unserialize($cache);

// Validate cache file
if ( ! isset($cache['exp'] ) OR ! isset($cache['headers']) OR ! isset($cache['output']))
{
return FALSE;
}

// Has the file expired? If so we’ll delete it.
if (time() >= trim($cache['exp']))
{
@unlink($filepath);
log_message(‘debug’, “Cache file has expired. File deleted”);
return FALSE;
}

// Restore headers
$this->headers = $cache['headers'];

// Display the cache
$this->_display($cache['output']);
log_message(‘debug’, “Cache file is current. Sending it to browser.”);
return TRUE;

}
}

Google Chrome插件推荐(顺便发送Google Wave邀请函)

前几天Google Chrome的官方插件站上线,今天去逛了一圈,发现已有不少插件上线了,现把我添加的几个插件展示下:

  • AdBlock+ Element Hiding Helper:去广告必备
  • Drag and Go:超级拖拽,纯属个人习惯
  • Firebug Lite:虽然功能不及火狐下的版本,但配合Chrome本身的管理员工具,也够用
  • Google Tasks:顾名思义,直接在工具栏中查看Tasks,同时也支持Google App
  • One Number:检查Gmail、Google Reader、Google Wave和Google Voice,相当好用
  • Xmarks Bookmarks Sync:保持各个浏览器的书签同步,唯一的遗憾是目前还不支持非Firefox的密码同步

更多插件请移步这里:https://chrome.google.com/extensions

如果需要Google Wave邀请函,请留言并填写正确的gmail地址

Codeigniter提速指南

Codeigniter是一款非常轻量级和易于上手的PHP框架,虽然它的运行速度并不慢,但我们还是可以通过一些措施来让它更快!

步骤1:启用Gzip

开启方法:修改config.php中的$config['compress_output'] = TRUE;

步骤2:启用Cache

开启方法:在controllers中添加$this->output->cache(n); n表示缓存的寿命为多少分钟,之后会重新生成新的缓存

步骤3:合并JS、CSS文件

不多叙述,我采用的方法是把常用的JS和CSS文件都存放在view里,需要使用时将它们合并后输出

步骤4:优化.htaccsee(Apache主机适用)

#设置各种类型文件的到期时间

ExpiresActive on
ExpiresByType text/css M78844000
ExpiresByType text/javascript M78844000
ExpiresByType image/gif M78844000
ExpiresByType image/jpeg M78844000
ExpiresByType image/png M78844000
ExpiresByType application/x-javascript M78844000
ExpiresByType */* M78844000

#清空ETag

Header unset ETag
FileETag None