<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>超群.com的博客 &#187; PHP</title>
	<atom:link href="http://www.fuchaoqun.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fuchaoqun.com</link>
	<description></description>
	<lastBuildDate>Thu, 08 Sep 2011 15:08:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>高性能LAMP程序设计</title>
		<link>http://www.fuchaoqun.com/2011/07/high-performance-lamp-website/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=high-performance-lamp-website</link>
		<comments>http://www.fuchaoqun.com/2011/07/high-performance-lamp-website/#comments</comments>
		<pubDate>Sun, 17 Jul 2011 23:51:29 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[Arch]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://www.fuchaoqun.com/?p=484</guid>
		<description><![CDATA[周六分享的PPT，一些比较common的大杂烩，看不到slides的同学在这里查看。]]></description>
			<content:encoded><![CDATA[<p>周六分享的PPT，一些比较common的大杂烩，看不到slides的同学在<a href="http://www.slideshare.net/fuchaoqun/lamp-8611126" target="_blank">这里</a>查看。<br />
<iframe src="http://www.slideshare.net/slideshow/embed_code/8611126?rel=0" width="425" height="355" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2011/07/high-performance-lamp-website/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>使用fastcgi_cache加速你的Nginx网站</title>
		<link>http://www.fuchaoqun.com/2011/01/nginx-fastcgi_cache/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nginx-fastcgi_cache</link>
		<comments>http://www.fuchaoqun.com/2011/01/nginx-fastcgi_cache/#comments</comments>
		<pubDate>Sat, 01 Jan 2011 03:39:17 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[fastcgi_cache]]></category>
		<category><![CDATA[nginx]]></category>
		<guid isPermaLink="false">http://www.fuchaoqun.com/?p=416</guid>
		<description><![CDATA[很久以前在TW上挖了个坑，说nginx的fastcgi_cache是被大家忽视的一大金矿，今天把这个坑填上，顺祝大家新年快乐。 对于变化不太频繁的数据，大家都比较喜欢存Memcached以减少数据库的读取，但还是会有语言解析运行上的消耗（比如运行PHP，Python等），当然这个时间很短，记得OP上有个同学说P字头的语言，效率都不高，如果能省去，当然最好。（已经用上Squid等的可以忽略本文）。 还有一个问题就是很多时候一个页面由多个数据片断组成，为了提高页面速度，要么分别缓存，要么整体缓存（所谓的Page Cache），其实都比较麻烦，增加和减少数据片断的时，大多需要调整。 最后一个问题，所有的数据都存Memcached是否经济？服务器资源足够多的无所谓，捉襟见肘的就要考虑了，当然，生成静态页面是一种方法，需要维护，还是比较累。 好吧，nginx的fastcgi_cache可以解决上面的那些问题，比较squid等的好处是简单，不需再要去维护另外一个系统，适合不那么大的网站。 关于Nginx fastcgi_cache，基础的可以参看Nginx官方文档http://wiki.nginx.org/HttpFcgiModule，下面是一个典型的做法是： fastcgi_temp_path /data/ngx_fcgi_tmp; fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g; fastcgi_cache_valid 200 301 302 1d; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_cache_key $request_method://$host$request_uri; 注意一定要加上$request_method作为cache key，否则如果HEAD类型的先请求会导致后面的GET请求返回为空，全局定义一个缓存空间，配置文件名为，fastcgi_cache.conf，然后在vhost配置里面加上： fastcgi_cache ngx_fcgi_cache; include fastcgi.conf; 大概解释下各个参数的含义： fastcgi_temp_path：生成fastcgi_cache临时文件目录 fastcgi_cache_path：fastcgi_cache缓存目录，可以设置目录哈希层级，比如2:2会生成256*256个字目录，keys_zone是这个缓存空间的名字，cache是用多少内存（这样热门的内容nginx直接放内存，提高访问速度），inactive表示默认失效时间，max_size表示最多用多少硬盘空间，需要注意的是fastcgi_cache缓存是先写在fastcgi_temp_path再移到fastcgi_cache_path，所以这两个目录最好在同一个分区，从0.8.9之后可以在不同的分区，不过还是建议放同一分区。 fastcgi_cache_valid：定义哪些http头要缓存 fastcgi_cache_use_stale：定义哪些情况下用过期缓存 fastcgi_cache_key：定义fastcgi_cache的key，示例中就以请求的URI作为缓存的key，Nginx会取这个key的md5作为缓存文件，如果设置了缓存哈希目录，Nginx会从后往前取相应的位数做为目录。 fastcgi_cache：用哪个缓存空间 这样就可以了，基本上可以work，但还没完，如何手动清除缓存？有个Nginx的第三方扩展可帮你做到：https://github.com/FRiCKLE/ngx_cache_purge/，如果对大多数第三方扩展无爱，写个清除的脚本也非常简单，以PHP为例： 1 2 3 4 5 6 7 8 9 10 11 12 13 [...]]]></description>
			<content:encoded><![CDATA[<p>很久以前在TW上挖了个坑，说nginx的fastcgi_cache是被大家忽视的一大金矿，今天把这个坑填上，顺祝大家新年快乐。</p>
<p>对于变化不太频繁的数据，大家都比较喜欢存Memcached以减少数据库的读取，但还是会有语言解析运行上的消耗（比如运行PHP，Python等），当然这个时间很短，记得OP上有个同学说P字头的语言，效率都不高，如果能省去，当然最好。（已经用上Squid等的可以忽略本文）。</p>
<p>还有一个问题就是很多时候一个页面由多个数据片断组成，为了提高页面速度，要么分别缓存，要么整体缓存（所谓的Page Cache），其实都比较麻烦，增加和减少数据片断的时，大多需要调整。</p>
<p>最后一个问题，所有的数据都存Memcached是否经济？服务器资源足够多的无所谓，捉襟见肘的就要考虑了，当然，生成静态页面是一种方法，需要维护，还是比较累。</p>
<p>好吧，nginx的fastcgi_cache可以解决上面的那些问题，比较squid等的好处是简单，不需再要去维护另外一个系统，适合不那么大的网站。</p>
<p>关于Nginx fastcgi_cache，基础的可以参看Nginx官方文档<a href="http://wiki.nginx.org/HttpFcgiModule" target="_blank">http://wiki.nginx.org/HttpFcgiModule</a>，下面是一个典型的做法是：</p>
<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">fastcgi_temp_path /data/ngx_fcgi_tmp<span style="color: #666666; font-style: italic;">;</span>
fastcgi_cache_path /data/ngx_fcgi_cache levels<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g</span><span style="color: #666666; font-style: italic;">;</span>
fastcgi_cache_valid <span style="">200</span> <span style="">301</span> <span style="">302</span> 1d<span style="color: #666666; font-style: italic;">;</span>
fastcgi_cache_use_stale error timeout invalid_header http_500<span style="color: #666666; font-style: italic;">;</span>
fastcgi_cache_key $request_method://$host$request_uri<span style="color: #666666; font-style: italic;">;</span></pre></div></div>
<p>注意一定要加上$request_method作为cache key，否则如果HEAD类型的先请求会导致后面的GET请求返回为空，全局定义一个缓存空间，配置文件名为，fastcgi_cache.conf，然后在vhost配置里面加上：</p>
<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">fastcgi_cache ngx_fcgi_cache<span style="color: #666666; font-style: italic;">;</span>
include fastcgi.conf<span style="color: #666666; font-style: italic;">;</span></pre></div></div>
<p>大概解释下各个参数的含义：</p>
<p>fastcgi_temp_path：生成fastcgi_cache临时文件目录</p>
<p>fastcgi_cache_path：fastcgi_cache缓存目录，可以设置目录哈希层级，比如2:2会生成256*256个字目录，keys_zone是这个缓存空间的名字，cache是用多少内存（这样热门的内容nginx直接放内存，提高访问速度），inactive表示默认失效时间，max_size表示最多用多少硬盘空间，需要注意的是fastcgi_cache缓存是先写在fastcgi_temp_path再移到fastcgi_cache_path，所以这两个目录最好在同一个分区，从0.8.9之后可以在不同的分区，不过还是建议放同一分区。</p>
<p>fastcgi_cache_valid：定义哪些http头要缓存</p>
<p>fastcgi_cache_use_stale：定义哪些情况下用过期缓存</p>
<p>fastcgi_cache_key：定义fastcgi_cache的key，示例中就以请求的URI作为缓存的key，Nginx会取这个key的md5作为缓存文件，如果设置了缓存哈希目录，Nginx会从后往前取相应的位数做为目录。</p>
<p>fastcgi_cache：用哪个缓存空间</p>
<p>这样就可以了，基本上可以work，但还没完，如何手动清除缓存？有个Nginx的第三方扩展可帮你做到：<a href="https://github.com/FRiCKLE/ngx_cache_purge/" target="_blank">https://github.com/FRiCKLE/ngx_cache_purge/</a>，如果对大多数第三方扩展无爱，写个清除的脚本也非常简单，以PHP为例：</p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> purgeCache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>Cola_Com_Validate<span style="color: #339933;">::</span><span style="color: #004000;">url</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'请输入正确的URL。'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$md5</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$cacheFile</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_cacheRoot <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$md5</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$md5</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$md5</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cacheFile</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'缓存不存在。'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cacheFile</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'清除缓存成功。'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'清除缓存失败。'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>
<p>核心是第11行，直接找到缓存文件，然后删掉就可以，这个脚本有个副作用，手动清除之后，缓存失效，但Nginx后面还会自己清除一遍，然后报个unlink失败的日志，不过无关紧要了。</p>
<p>淡定，文章还没完，要不就成标题党了，Nginx fastcgi_cache缓存很不错，但我只想在某些页面用fastcgi_cache，很简单，有两种方法，一是在location中定义fastcgi_cache，这样只有满足一定规则的url才会用上cache，其他的就不会了;另外一种方法是在你不需要缓存的页面上，输出禁止缓存的头信息，用ColaPHP的话，直接$this->response->disableBrowserCache(); 具体代码：</p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Last-Modified: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">gmdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D, d M Y H:i:s&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; GMT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: no-store, no-cache, must-revalidate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: post-check=0, pre-check=0&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Pragma: no-cache&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>
<p>这样就告诉Nginx，这个页面不需要缓存。</p>
<p>好吧，要淡定不要D疼，还有最后一个问题，如果页面中只有一小部分内容不可以缓存，可以用Nginx fastcgi_cache吗？比如某个内容页，大部分内容可以缓存，但希望把用户的登录信息更新上去。答案是肯定的，可以直接输出用户未登录的页面样式，等页面加载完毕之后，通过ajax异步更新用户信息：</p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    initUser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>
<p>码完收工。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2011/01/nginx-fastcgi_cache/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>ColaPHP 1.0GA发布</title>
		<link>http://www.fuchaoqun.com/2010/11/colaphp-1-0-ga/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=colaphp-1-0-ga</link>
		<comments>http://www.fuchaoqun.com/2010/11/colaphp-1-0-ga/#comments</comments>
		<pubDate>Sat, 27 Nov 2010 03:50:29 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[ColaPHP]]></category>
		<guid isPermaLink="false">http://www.fuchaoqun.com/?p=408</guid>
		<description><![CDATA[从2008年底开始，经历了4个alpha版本、5个beta版本，趁着今天下午去Beijing Open Party，ColaPHP发布1.0GA版本，这是ColaPHP一个重要的里程碑版本，相信我，这只是开始&#8230;. 感兴趣的同学可以到http://code.google.com/p/colaphp/下载1.0GA的版本，当然，欢迎你下午和我一起交流。]]></description>
			<content:encoded><![CDATA[<p>从2008年底开始，经历了4个alpha版本、5个beta版本，趁着今天下午去<a href="http://www.beijing-open-party.org/topic/38" target="_blank">Beijing Open Party</a>，ColaPHP发布1.0GA版本，这是ColaPHP一个重要的里程碑版本，相信我，这只是开始&#8230;.</p>
<p>感兴趣的同学可以到<a href="http://code.google.com/p/colaphp/" target="_blank">http://code.google.com/p/colaphp/</a>下载1.0GA的版本，当然，欢迎你下午和我一起交流。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2010/11/colaphp-1-0-ga/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ColaPHP In Action</title>
		<link>http://www.fuchaoqun.com/2010/08/colaphp-in-action/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=colaphp-in-action</link>
		<comments>http://www.fuchaoqun.com/2010/08/colaphp-in-action/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 05:39:17 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[ColaPHP]]></category>
		<guid isPermaLink="false">http://www.fuchaoqun.com/?p=402</guid>
		<description><![CDATA[看不到Slide的同学，可以直接点这里。]]></description>
			<content:encoded><![CDATA[<div style="width:425px" id="__ss_4905385"><object id="__sse4905385" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=colaphpinaction-100805004329-phpapp02&#038;stripped_title=colaphp-in-action" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse4905385" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=colaphpinaction-100805004329-phpapp02&#038;stripped_title=colaphp-in-action" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object></div>
<p>看不到Slide的同学，可以直接点<a href="http://www.fuchaoqun.com/2010/08/colaphp-in-action/" target="_blank">这里</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2010/08/colaphp-in-action/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Mediawiki扩展编写实战</title>
		<link>http://www.fuchaoqun.com/2010/07/mediawiki-extension/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mediawiki-extension</link>
		<comments>http://www.fuchaoqun.com/2010/07/mediawiki-extension/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 11:25:50 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[mediawiki]]></category>
		<guid isPermaLink="false">http://www.fuchaoqun.com/?p=381</guid>
		<description><![CDATA[Wikipedia大家都很熟悉，而Mediawiki则是Wikipedia背后的功臣，整个Wikipedia都构建在mediawiki之上，mediawiki的稳定性和高效性值得信赖，同时Mediawiki非常易于扩展，可以通过Extension的方式添加非常多的功能，而且Mediawiki的Extension社区也非常活跃，大家可以到Mediawiki Extension目录下去下载自己需要的扩展程序。 上周末，帮朋友写了一些Mediawiki的扩展，立即被Mediawiki的强大扩展性折服，主要实现的功能有：增加Google Analytics统计、自定义标题、增加Google Adsense广告之类，写Mediawiki的扩展，最好的参考是Mediawiki扩展手册：http://www.mediawiki.org/wiki/Manual:Extensions。 Mediawiki的扩展主要有Tag Extension、Parser Functions、Hooks、Special Pages、Skins、Magic Words，对应的中文是：标签扩展（自定义wiki标签，比如xxxx）、解析扩展（和标签类似，不过呈现方式稍有不通，为{{#foo : bar}}）、钩子、特殊页面、皮肤、魔术关键字，我这里演示的是Parser Functions和Hooks，其他的差不多类似。 一、增加Google Analytics统计和Google Adsense广告 原理很简单，我们在页面显示之前，把Google Analytics和Google Adsense的代码append到要显示的内容即可，代码： &#60;?php /** * 安全设置，防止恶意调用 */ if &#40;!defined&#40;'MEDIAWIKI'&#41;&#41; &#123; die&#40; 'This file is a MediaWiki extension, it is not a valid entry point' &#41;; &#125; &#160; /** * 扩展的基本信息 */ $wgExtensionCredits&#91;'other'&#93;&#91;&#93; = array&#40; 'path' =&#62; __FILE__, 'name' [...]]]></description>
			<content:encoded><![CDATA[<p>Wikipedia大家都很熟悉，而<a href="http://www.mediawiki.org/" target="_blank">Mediawiki</a>则是Wikipedia背后的功臣，整个Wikipedia都构建在mediawiki之上，mediawiki的稳定性和高效性值得信赖，同时Mediawiki非常易于扩展，可以通过Extension的方式添加非常多的功能，而且Mediawiki的Extension社区也非常活跃，大家可以到<a href="http://www.mediawiki.org/wiki/Category:Extensions" target="_blank">Mediawiki Extension</a>目录下去下载自己需要的扩展程序。</p>
<p>上周末，帮朋友写了一些Mediawiki的扩展，立即被Mediawiki的强大扩展性折服，主要实现的功能有：增加Google Analytics统计、自定义标题、增加Google Adsense广告之类，写Mediawiki的扩展，最好的参考是Mediawiki扩展手册：<a href="http://www.mediawiki.org/wiki/Manual:Extensions" target="_blank">http://www.mediawiki.org/wiki/Manual:Extensions</a>。</p>
<p>Mediawiki的扩展主要有Tag Extension、Parser Functions、Hooks、Special Pages、Skins、Magic Words，对应的中文是：标签扩展（自定义wiki标签，比如<foo>xxxx</foo>）、解析扩展（和标签类似，不过呈现方式稍有不通，为{{#foo : bar}}）、钩子、特殊页面、皮肤、魔术关键字，我这里演示的是Parser Functions和Hooks，其他的差不多类似。</p>
<p>一、增加Google Analytics统计和Google Adsense广告</p>
<p>原理很简单，我们在页面显示之前，把Google Analytics和Google Adsense的代码append到要显示的内容即可，代码：</p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * 安全设置，防止恶意调用
 */</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MEDIAWIKI'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'This file is a MediaWiki extension, it is not a valid entry point'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * 扩展的基本信息
 */</span>
<span style="color: #000088;">$wgExtensionCredits</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'other'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'path'</span>           <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'name'</span>           <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'插件名称'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'version'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1.0'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'author'</span>         <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'作者'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'descriptionmsg'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'简要说明'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'url'</span>            <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'作者地址'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * 注册一个钩子，在页面显示之前，处理页面显示内容
 *
 * 全部钩子列表：http://www.mediawiki.org/wiki/Manual:Hooks
 *
 */</span>
<span style="color: #000088;">$wgHooks</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'BeforePageDisplay'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'dzBeforePageDisplay'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> dzBeforePageDisplay<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$out</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$skin</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * 在LocalSettings.php定义$wgDangZhiAppendHtml
     * 把要添加的Google Analytics和Google Adsense代码放里面
     */</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wgDangZhiAppendHtml</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// 页面添加HTML</span>
    <span style="color: #000088;">$out</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addHTML</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wgDangZhiAppendHtml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// 记得返回true，收工</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>
<p>短短几行代码，完成添加Google Analytics统计和Google Adsense广告代码，当然，你还可以添加任意你想添加到网页最后的代码，可以查看<a href="http://www.dangzhi.com" target="_blank">示例网站</a>的源代码，Google Analytics的代码布在每个页面上。</p>
<p>二、自定义文章标题</p>
<p>自定义文章标题对搜索引擎来说非常重要，Mediawiki默认的标题只能和内容条目一样，缺乏灵活性，这里的原理是增加一个{{#title: 标题内容}}的标签，在页面显示之前替换成网页标题，<a href="http://www.dangzhi.com" target="_blank">示例网站</a>的首页就自定了HTML标题。</p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * 还是第一示例中的显示前处理函数，这里是部分内容
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> dzBeforePageDisplay<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$out</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$skin</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// 如果页面中存在{{#title: 标题内容}}，那么指定文章标题</span>
    <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\{\{#title: ?(.*)\}\}/U'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$out</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mBodytext</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$out</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mBodytext</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$out</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mBodytext</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$out</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mHTMLtitle</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>
<p>三、文章在新窗口打开</p>
<p>Mediawiki是外国人做的东西，国外少有默认新窗口打开链接的习惯，国人的习惯可不是这样，让Mediawiki默认新窗口打开的做法非常的简单。</p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * 还记得这个函数吧
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> dzBeforePageDisplay<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$out</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$skin</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// 新窗口打开链接</span>
    <span style="color: #000088;">$p</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/(&lt;a href=\&quot;\/index.php\?title=.+\&quot;)/U'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/(&lt;a href=\&quot;\/wiki\/.+\&quot;)/U'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'$1 target=&quot;_blank&quot;'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$out</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mBodytext</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #339933;">,</span> <span style="color: #000088;">$r</span><span style="color: #339933;">,</span> <span style="color: #000088;">$out</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mBodytext</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>
<p>在页面输出之前，给需要的a标签加个target=&#8221;_blank&#8221;的属性即可。</p>
<p>四、解析HTML标签</p>
<p>Mediawiki为了安全考虑，不允许在内容中使用非安全HTML标签，我们可以使用一种加密HTML的方法来达到安全性和易用性的平衡。在内容中增加{{#html: 加密码 | html内容}}，其中加密码是系统密钥和html内容的md5值，如果符合则显示。</p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * 初始化Parser Functions
 */</span>
<span style="color: #000088;">$wgHooks</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ParserFirstCallInit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'dzParserSetup'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$wgHooks</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'LanguageGetMagic'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'dzMagicSetup'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * 设置解析函数
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> dzParserSetup<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$parser</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$parser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFunctionHook</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'html'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'dzParseHtml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// 可以设置多个，$parser-&gt;setFunctionHook('foo', 'dzParseFoo');</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * 初始化Parser关键字
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> dzMagicSetup<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$magicWords</span><span style="color: #339933;">,</span> <span style="color: #000088;">$langCode</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$magicWords</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'html'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// 可以设置多个，$magicWords['foo'] = array(0, 'foo');</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * 解析HTML
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> dzParseHtml<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$param1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$param2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// 在LocalSettings.php定义HTML加密密钥</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wgHtmlSalt</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wgHtmlSalt</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$param2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$param1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$param2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'noparse'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'isHTML'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'bad html'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>
<p>Mediawiki的扩展写起来还是相当容易，文中提到的效果可以在<a href="http://www.dangzhi.com" target="_blank">这里体验</a>，老外写的东西对Extension都很重视，wordpress也是这样，不似国内的某些项目，稍做修改都得hack代码，hack的坏处是升级的时候容易悲剧。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2010/07/mediawiki-extension/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ColaPHP 0.8beta发布</title>
		<link>http://www.fuchaoqun.com/2010/07/colaphp-0-8-beta/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=colaphp-0-8-beta</link>
		<comments>http://www.fuchaoqun.com/2010/07/colaphp-0-8-beta/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 16:31:52 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[ColaPHP]]></category>
		<guid isPermaLink="false">http://www.fuchaoqun.com/?p=378</guid>
		<description><![CDATA[代号：Mini，bugfix版本，改善框架易用性，代码重构。 下载ColaPHP 0.8beta，阅读ColaPHP文档，访问ColaPHP项目。 ColaPHP 0.8beta已经很稳定，可以在生产环境使用。]]></description>
			<content:encoded><![CDATA[<p>代号：Mini，bugfix版本，改善框架易用性，代码重构。</p>
<p><a href="http://colaphp.googlecode.com/files/ColaPHP-0.8-beta.zip" target="_blank">下载ColaPHP 0.8beta</a>，阅读<a href="http://code.google.com/p/colaphp/w/list" target="_blank">ColaPHP文档</a>，访问<a href="http://code.google.com/p/colaphp/" target="_blank">ColaPHP项目</a>。</p>
<p>ColaPHP 0.8beta已经很稳定，可以在生产环境使用。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2010/07/colaphp-0-8-beta/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP导出MySQL数据到Excel文件</title>
		<link>http://www.fuchaoqun.com/2010/05/php-export-mysql-excel/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=php-export-mysql-excel</link>
		<comments>http://www.fuchaoqun.com/2010/05/php-export-mysql-excel/#comments</comments>
		<pubDate>Wed, 12 May 2010 01:37:07 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[excel]]></category>
		<guid isPermaLink="false">http://www.fuchaoqun.com/?p=352</guid>
		<description><![CDATA[经常会碰到需要从数据库中导出数据到Excel文件，用一些开源的类库，比如PHPExcel，确实比较容易实现，但对大量数据的支持很不好，很容易到达PHP内存使用上限。这里的方法是利用fputcsv写CSV文件的方法，直接向浏览器输出Excel文件。 &#60;?php // 输出Excel文件头，可把user.csv换成你要的文件名 header&#40;'Content-Type: application/vnd.ms-excel'&#41;; header&#40;'Content-Disposition: attachment;filename=&#34;user.csv&#34;'&#41;; header&#40;'Cache-Control: max-age=0'&#41;; &#160; // 从数据库中获取数据，为了节省内存，不要把数据一次性读到内存，从句柄中一行一行读即可 $sql = 'select * from tbl where ……'; $stmt = $db-&#62;query&#40;$sql&#41;; &#160; // 打开PHP文件句柄，php://output 表示直接输出到浏览器 $fp = fopen&#40;'php://output', 'a'&#41;; &#160; // 输出Excel列名信息 $head = array&#40;'姓名', '性别', '年龄', 'Email', '电话', '……'&#41;; foreach &#40;$head as $i =&#62; $v&#41; &#123; // CSV的Excel支持GBK编码，一定要转换，否则乱码 $head&#91;$i&#93; = iconv&#40;'utf-8', [...]]]></description>
			<content:encoded><![CDATA[<p>经常会碰到需要从数据库中导出数据到Excel文件，用一些开源的类库，比如<a href="http://phpexcel.codeplex.com/" target="_blank">PHPExcel</a>，确实比较容易实现，但对大量数据的支持很不好，很容易到达PHP内存使用上限。这里的方法是利用fputcsv写CSV文件的方法，直接向浏览器输出Excel文件。</p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// 输出Excel文件头，可把user.csv换成你要的文件名</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: application/vnd.ms-excel'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Disposition: attachment;filename=&quot;user.csv&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cache-Control: max-age=0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 从数据库中获取数据，为了节省内存，不要把数据一次性读到内存，从句柄中一行一行读即可</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'select * from tbl where ……'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 打开PHP文件句柄，php://output 表示直接输出到浏览器</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'php://output'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 输出Excel列名信息</span>
<span style="color: #000088;">$head</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'姓名'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'性别'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'年龄'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Email'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'电话'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'……'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$head</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// CSV的Excel支持GBK编码，一定要转换，否则乱码</span>
    <span style="color: #000088;">$head</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">iconv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'utf-8'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gbk'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 将数据通过fputcsv写到文件句柄</span>
<span style="color: #990000;">fputcsv</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$head</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 计数器</span>
<span style="color: #000088;">$cnt</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// 每隔$limit行，刷新一下输出buffer，不要太大，也不要太小</span>
<span style="color: #000088;">$limit</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100000</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 逐行取出数据，不浪费内存</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch</span><span style="color: #009900;">&#40;</span>Zend_Db<span style="color: #339933;">::</span><span style="color: #004000;">FETCH_NUM</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$cnt</span> <span style="color: #339933;">++;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$limit</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$cnt</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//刷新一下输出buffer，防止由于数据过多造成问题</span>
        <span style="color: #990000;">ob_flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$cnt</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">iconv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'utf-8'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gbk'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #990000;">fputcsv</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>
<p>简单易用，非常节省内存，不依赖第三方类库。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2010/05/php-export-mysql-excel/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ColaPHP 0.7beta发布</title>
		<link>http://www.fuchaoqun.com/2010/04/colaphp-0-7beta/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=colaphp-0-7beta</link>
		<comments>http://www.fuchaoqun.com/2010/04/colaphp-0-7beta/#comments</comments>
		<pubDate>Sat, 03 Apr 2010 06:36:47 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://www.fuchaoqun.com/?p=341</guid>
		<description><![CDATA[ColaPHP的2010.3月度发布计划版本，稍微晚了几天，代号Recode，和0.6beta相比,框架结构有比较大的变化，主要修改如下： 精简框架核心，除FrontController、Router、MVC外，其他功能组件化 Cola_Db、Cola_Cache、Cola_Log、Cola_Yaml已组件化成Cola_Com_Db、Cola_Com_Cache、Cola_Com_Log、Cola_Com_Yaml Controller中可直接使用$this-&#62;com-&#62;db($config)之类的接口来调用组件 少量代码重构以及bug fix 下载ColaPHP 0.7beta，阅读ColaPHP文档，访问ColaPHP项目。 由于0.7beta做框架架构上变化有点大，不建议立即采用，生产环境中建议使用稳定的0.6beta，下一个版本0.8beta开发代号：Mini，主要对架构、代码做进一步重构优化。]]></description>
			<content:encoded><![CDATA[<p>ColaPHP的2010.3月度发布计划版本，稍微晚了几天，代号Recode，和0.6beta相比,框架结构有比较大的变化，主要修改如下：</p>
<ul>
<li>精简框架核心，除FrontController、Router、MVC外，其他功能组件化</li>
<li>Cola_Db、Cola_Cache、Cola_Log、Cola_Yaml已组件化成Cola_Com_Db、Cola_Com_Cache、Cola_Com_Log、Cola_Com_Yaml</li>
<li>Controller中可直接使用$this-&gt;com-&gt;db($config)之类的接口来调用组件</li>
<li>少量代码重构以及bug fix</li>
</ul>
<p><a href="http://colaphp.googlecode.com/files/ColaPHP-0.7-beta.zip" target="_blank">下载ColaPHP 0.7beta</a>，阅读<a href="http://code.google.com/p/colaphp/w/list" target="_blank">ColaPHP文档</a>，访问<a href="http://code.google.com/p/colaphp/" target="_blank">ColaPHP项目</a>。</p>
<p>由于0.7beta做框架架构上变化有点大，不建议立即采用，生产环境中建议使用稳定的0.6beta，下一个版本0.8beta开发代号：Mini，主要对架构、代码做进一步重构优化。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2010/04/colaphp-0-7beta/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ColaPHP 0.6beta发布</title>
		<link>http://www.fuchaoqun.com/2010/02/colaphp-0-6-beta/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=colaphp-0-6-beta</link>
		<comments>http://www.fuchaoqun.com/2010/02/colaphp-0-6-beta/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 17:39:33 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ColaPHP]]></category>
		<guid isPermaLink="false">http://www.fuchaoqun.com/?p=330</guid>
		<description><![CDATA[ColaPHP月度发布计划版本，代号：Easy，和0.5beta相比变化不大，主要修改如下： 增加Yaml处理，底层调用symfony yaml包处理 增加自定义异常类，后续准备将框架中的异常细分（好处是将来可以对异常做针对性处理） 少量代码重构以及bug fix 下载ColaPHP 0.6beta，阅读ColaPHP文档，访问ColaPHP项目。 下一个版本0.7beta开发代号：Recode，主要对代码做进一步重构优化。]]></description>
			<content:encoded><![CDATA[<p>ColaPHP月度发布计划版本，代号：Easy，和0.5beta相比变化不大，主要修改如下：</p>
<ul>
<li>增加Yaml处理，底层调用symfony yaml包处理</li>
<li> 增加自定义异常类，后续准备将框架中的异常细分（好处是将来可以对异常做针对性处理）</li>
<li> 少量代码重构以及bug fix</li>
</ul>
<p><a href="http://colaphp.googlecode.com/files/ColaPHP-0.6-beta.zip" target="_blank">下载ColaPHP 0.6beta</a>，阅读<a href="http://code.google.com/p/colaphp/w/list" target="_blank">ColaPHP文档</a>，访问<a href="http://code.google.com/p/colaphp/" target="_blank">ColaPHP项目</a>。</p>
<p>下一个版本0.7beta开发代号：Recode，主要对代码做进一步重构优化。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2010/02/colaphp-0-6-beta/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ColaPHP-0.4-alpha发布</title>
		<link>http://www.fuchaoqun.com/2009/12/colaphp-0-4-alpha/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=colaphp-0-4-alpha</link>
		<comments>http://www.fuchaoqun.com/2009/12/colaphp-0-4-alpha/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 13:46:59 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[ColaPHP]]></category>
		<category><![CDATA[PHP Framework]]></category>
		<guid isPermaLink="false">http://www.fuchaoqun.com/?p=279</guid>
		<description><![CDATA[ColaPHP月度发布计划，基本上每月发一个release，相比较0.3alpha，比较大的修改如下： 增加了动态路由模式，可不用定义URL规则 增加对MySQL Master-Slave支持，可以由单数据库无缝迁移到主从模式 去除框架中Smarty模板的绑定，可以在Controller中自行调用Smarty模板 Helper中增加了性能测试模块Benchmark.php 增加对MongoDB的简单绑定Mongo.php 性能进一步提升 大量的代码重构以及bug fix 访问ColaPHP官方网站，下载0.4alpha，不过建议随时跟进我们的svn://colaphp.googlecode.com/svn/trunk/，ColaPHP一直在活跃开发。 ColaPHP 0.4alpha已完成预期目标，所有的函数都控制在20行以内。下一个版本0.5alpha版本开发代号：Practice，ColaPHP已经在一些实际项目中使用，0.5alpha将得到更多的实践优化 招募PHP极客加入ColaPHP，联系fuchaoqun#gmail.com。]]></description>
			<content:encoded><![CDATA[<p>ColaPHP月度发布计划，基本上每月发一个release，相比较0.3alpha，比较大的修改如下：</p>
<ul>
<li>增加了动态路由模式，可不用定义URL规则</li>
<li>增加对MySQL Master-Slave支持，可以由单数据库无缝迁移到主从模式</li>
<li>去除框架中Smarty模板的绑定，可以在Controller中自行调用Smarty模板</li>
<li>Helper中增加了性能测试模块Benchmark.php</li>
<li>增加对MongoDB的简单绑定Mongo.php</li>
<li>性能进一步提升</li>
<li>大量的代码重构以及bug fix </li>
</ul>
<p>访问<a href="http://code.google.com/p/colaphp/" target="_blank">ColaPHP</a>官方网站，下载<a href="http://colaphp.googlecode.com/files/ColaPHP-0.4-alpha.zip" target="_blank">0.4alpha</a>，不过建议随时跟进我们的svn://colaphp.googlecode.com/svn/trunk/，ColaPHP一直在活跃开发。</p>
<p>ColaPHP 0.4alpha已完成预期目标，所有的函数都控制在20行以内。下一个版本0.5alpha版本开发代号：Practice，ColaPHP已经在一些实际项目中使用，0.5alpha将得到更多的实践优化</p>
<p>招募PHP极客加入ColaPHP，联系fuchaoqun#gmail.com。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2009/12/colaphp-0-4-alpha/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
