<?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>ls /proc &#187; php</title>
	<atom:link href="http://www.lsproc.com/blog/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lsproc.com/blog</link>
	<description>lsproc.com</description>
	<lastBuildDate>Fri, 21 May 2010 14:29:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo</title>
		<link>http://www.lsproc.com/blog/nginx_php_pathinfo_securit/</link>
		<comments>http://www.lsproc.com/blog/nginx_php_pathinfo_securit/#comments</comments>
		<pubDate>Fri, 21 May 2010 03:34:47 +0000</pubDate>
		<dc:creator>lostsnow</dc:creator>
				<category><![CDATA[Program&Database]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[pathinfo]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://www.lsproc.com/blog/?p=410</guid>
		<description><![CDATA[转载时请标明文章原始出处和作者信息, 作者: lostsnow.http://www.lsproc.com/blog/nginx_php_pathinfo_securit/
漏洞介绍：nginx是一款高性能的web服务器，使用非常广泛，其不仅经常被用作反向代理，也可以非常好的支持PHP的运行。80sec发现其中存在一个较为严重的安全问题，默认情况下可能导致服务器错误的将任何类型的文件以PHP的方式进行解析，这将导致严重的安全问题，使得恶意的攻击者可能攻陷支持php的nginx服务器。
漏洞分析：nginx默认以cgi的方式支持php的运行，譬如在配置文件当中可以以

location ~ \.php$ {
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    include fastcgi_params;
}

的方式支持对php的解析，location对请求进行选择的时候会使用URI环境变量进行选择，其中传递到后端Fastcgi的关键变量 SCRIPT_FILENAME由nginx生成的$fastcgi_script_name决定，而通过分析可以看到$fastcgi_script_name是直接由URI环境变量控制的，这里就是产生问题的点。而为了较好的支持PATH_INFO的提取，在PHP 的配置选项里存在cgi.fix_pathinfo选项，其目的是为了从SCRIPT_FILENAME里取出真正的脚本名。
那么假设存在一个http://www.lsproc.com/a.jpg，我们以如下的方式去访问


http://www.lsproc.com/a.jpg/xxx.php

将会得到一个URI

/a.jpg/xxx.php

经过location指令，该请求将会交给后端的fastcgi处理，nginx为其设置环境变量SCRIPT_FILENAME，内容为

/scripts/a.jpg/xxx.php

而在其他的webserver如lighttpd当中，我们发现其中的SCRIPT_FILENAME被正确的设置为

/scripts/a.jpg

所以不存在此问题。
后端的fastcgi在接受到该选项时，会根据fix_pathinfo配置决定是否对SCRIPT_FILENAME进行额外的处理，一般情况下如果不对fix_pathinfo进行设置将影响使用PATH_INFO进行路由选择的应用，所以该选项一般配置开启。Php通过该选项之后将查找其中真正的脚本文件名字，查找的方式也是查看文件是否存在，这个时候将分离出SCRIPT_FILENAME和PATH_INFO分别为

/scripts/a.jpg和xxx.php

最后，以/scripts/a.jpg作为此次请求需要执行的脚本，攻击者就可以实现让nginx以php来解析任何类型的文件了。
PHP为什么会接受这样的参数, 并且把a.jpg解析呢?
这就要说到PHP的cgi SAPI中的参数, fix_pathinfo了:

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP&#039;s
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to [...]]]></description>
			<content:encoded><![CDATA[<p>转载时请标明文章原始出处和作者信息, 作者: <a href="http://www.lsproc.com/blog/">lostsnow</a>.<br /><a href="http://www.lsproc.com/blog/nginx_php_pathinfo_securit/">http://www.lsproc.com/blog/nginx_php_pathinfo_securit/</a></p>
<p>漏洞介绍：nginx是一款高性能的web服务器，使用非常广泛，其不仅经常被用作反向代理，也可以非常好的支持PHP的运行。80sec发现其中存在一个较为严重的安全问题，默认情况下可能导致服务器错误的将任何类型的文件以PHP的方式进行解析，这将导致严重的安全问题，使得恶意的攻击者可能攻陷支持php的nginx服务器。</p>
<p>漏洞分析：nginx默认以cgi的方式支持php的运行，譬如在配置文件当中可以以</p>
<pre class="brush: text">
location ~ \.php$ {
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    include fastcgi_params;
}
</pre>
<p>的方式支持对php的解析，location对请求进行选择的时候会使用URI环境变量进行选择，其中传递到后端Fastcgi的关键变量 SCRIPT_FILENAME由nginx生成的$fastcgi_script_name决定，而通过分析可以看到$fastcgi_script_name是直接由URI环境变量控制的，这里就是产生问题的点。而为了较好的支持PATH_INFO的提取，在PHP 的配置选项里存在cgi.fix_pathinfo选项，其目的是为了从SCRIPT_FILENAME里取出真正的脚本名。<br />
那么假设存在一个http://www.lsproc.com/a.jpg，我们以如下的方式去访问</p>
<pre class="brush: text">

http://www.lsproc.com/a.jpg/xxx.php
</pre>
<p>将会得到一个URI</p>
<pre class="brush: text">
/a.jpg/xxx.php
</pre>
<p>经过location指令，该请求将会交给后端的fastcgi处理，nginx为其设置环境变量SCRIPT_FILENAME，内容为</p>
<pre class="brush: text">
/scripts/a.jpg/xxx.php
</pre>
<p>而在其他的webserver如lighttpd当中，我们发现其中的SCRIPT_FILENAME被正确的设置为</p>
<pre class="brush: text">
/scripts/a.jpg
</pre>
<p>所以不存在此问题。<br />
后端的fastcgi在接受到该选项时，会根据fix_pathinfo配置决定是否对SCRIPT_FILENAME进行额外的处理，一般情况下如果不对fix_pathinfo进行设置将影响使用PATH_INFO进行路由选择的应用，所以该选项一般配置开启。Php通过该选项之后将查找其中真正的脚本文件名字，查找的方式也是查看文件是否存在，这个时候将分离出SCRIPT_FILENAME和PATH_INFO分别为</p>
<pre class="brush: text">
/scripts/a.jpg和xxx.php
</pre>
<p>最后，以/scripts/a.jpg作为此次请求需要执行的脚本，攻击者就可以实现让nginx以php来解析任何类型的文件了。</p>
<p>PHP为什么会接受这样的参数, 并且把a.jpg解析呢?<br />
这就要说到PHP的cgi SAPI中的参数, fix_pathinfo了:</p>
<pre class="brush: text">
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP&#039;s
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix it&#039;s paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
cgi.fix_pathinfo=1
</pre>
<p>如果开启了这个选项, 那么就会触发在PHP中的如下逻辑:</p>
<pre class="brush: php">
/*
 * if the file doesn&#039;t exist, try to extract PATH_INFO out
 * of it by stat&#039;ing back through the &#039;/&#039;
 * this fixes url&#039;s like /info.php/test
 */
if (script_path_translated &amp;&amp;
     (script_path_translated_len = strlen(script_path_translated)) &gt; 0 &amp;&amp;
     (script_path_translated[script_path_translated_len-1] == &#039;/&#039; ||
//....以下省略.
</pre>
<p>到这里, PHP会认为SCRIPT_FILENAME是a.jpg, 而xxx.php是PATH_INFO, 然后PHP就把a.jpg当作一个PHP文件来解释执行… So…</p>
<p>POC： 访问一个nginx来支持php的站点，在一个任何资源的文件如robots.txt后面加上/xxx.php，这个时候你可以看到如下的区别：</p>
<p>访问http://www.lsproc.com/robots.txt</p>
<pre class="brush: text">
HTTP/1.1 200 OK
Server: nginx/0.6.32
Date: Thu, 20 May 2010 10:05:30 GMT
Content-Type: text/plain
Content-Length: 18
Last-Modified: Thu, 20 May 2010 06:26:34 GMT
Connection: keep-alive
Keep-Alive: timeout=20
Accept-Ranges: bytes
</pre>
<p>访问http://www.lsproc.com/robots.txt/xxx.php</p>
<pre class="brush: text">
HTTP/1.1 200 OK
Server: nginx/0.6.32
Date: Thu, 20 May 2010 10:06:49 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Keep-Alive: timeout=20
X-Powered-By: PHP/5.2.6
</pre>
<p>其中的Content-Type的变化说明了后端负责解析的变化，该站点就可能存在漏洞。</p>
<p>漏洞厂商：http://www.nginx.org</p>
<p>解决方案：</p>
<p>1. 修改php.ini中的cgi.fix_pathinfo为0 (即使你在php.ini中没有找到，也要设置，默认为1)<br />
2. 把nginx的判断正则修改为去除/</p>
<pre class="brush: text">
if ( $fastcgi_script_name ~ \..*\/.*php ) {
    return 403;
}
</pre>
<p>3. 如果上传的文件类型为图片, 使用 gd/imagemagick 等进行处理后再保存, 原始文件务必删除<br />
4. 上传的文件放到一个静态文件server, 此 server 不允许php 文件执行</p>
<p>本文参考链接</p>
<ul>
<li><a href="http://www.80sec.com/nginx-securit.html">http://www.80sec.com/nginx-securit.html</a></li>
<li><a href="http://www.laruence.com/2010/05/20/1495.html">http://www.laruence.com/2010/05/20/1495.html</a></li>
<li><a href="http://www.54chen.com/php-tech/nginx-php-cgi-of-security-hole.html">http://www.54chen.com/php-tech/nginx-php-cgi-of-security-hole.html</a></li>
</ul>
<p>-- EOF --</p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>2010-04-12 -- <a href="http://www.lsproc.com/blog/nginx_userid_decode/" title="nginx userid 模块客户端 cookie 解码">nginx userid 模块客户端 cookie 解码</a> (0)</li><li>2010-03-08 -- <a href="http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/" title="nginx+factcgi 下使用 ob_flush">nginx+factcgi 下使用 ob_flush</a> (0)</li><li>2009-03-29 -- <a href="http://www.lsproc.com/blog/nginx_ssl_config/" title="Nginx SSL 配置">Nginx SSL 配置</a> (22)</li><li>2008-10-23 -- <a href="http://www.lsproc.com/blog/nginx_rewrite_param/" title="nginx rewrite 的一些参数  ">nginx rewrite 的一些参数  </a> (1)</li><li>2008-06-15 -- <a href="http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/" title="lighttpd + PHP(fastcgi) 配置">lighttpd + PHP(fastcgi) 配置</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lsproc.com/blog/nginx_php_pathinfo_securit/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>nginx userid 模块客户端 cookie 解码</title>
		<link>http://www.lsproc.com/blog/nginx_userid_decode/</link>
		<comments>http://www.lsproc.com/blog/nginx_userid_decode/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 01:59:32 +0000</pubDate>
		<dc:creator>lostsnow</dc:creator>
				<category><![CDATA[Program&Database]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[decode]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.lsproc.com/blog/?p=387</guid>
		<description><![CDATA[转载时请标明文章原始出处和作者信息, 作者: lostsnow.http://www.lsproc.com/blog/nginx_userid_decode/
在网上看到用 ruby 解码的一段程序
http://forum.nginx.org/read.php?2,52592,52592

&#62; cookie_uid = &#34;0Cvz4ktwVPEdbRcMAwMFAg==&#34;; cc = cookie_uid.unpack(&#039;m*&#039;).first; rr = cc.split(&#34;&#34;).map{&#124;c&#124; c[0].to_i}.inject([]) {&#124;v,s&#124; v.push sprintf(&#34;%02X&#34;, s); v; }.values_at(3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12).join(&#34;&#34;)
=&#62; &#34;E2F32BD0F154704B0C176D1D02050303&#34;

我用 PHP 写了一个

&#60;?php
function nginx_userid_decode($str)
{
    $str_unpacked =  unpack(&#039;h*&#039;, base64_decode(str_replace(&#039; &#039;, &#039;+&#039;, $str)));
    $str_split [...]]]></description>
			<content:encoded><![CDATA[<p>转载时请标明文章原始出处和作者信息, 作者: <a href="http://www.lsproc.com/blog/">lostsnow</a>.<br /><a href="http://www.lsproc.com/blog/nginx_userid_decode/">http://www.lsproc.com/blog/nginx_userid_decode/</a></p>
<p>在网上看到用 ruby 解码的一段程序<br />
<a href="http://forum.nginx.org/read.php?2,52592,52592">http://forum.nginx.org/read.php?2,52592,52592</a></p>
<pre class="brush: ruby">
&gt; cookie_uid = &quot;0Cvz4ktwVPEdbRcMAwMFAg==&quot;; cc = cookie_uid.unpack(&#039;m*&#039;).first; rr = cc.split(&quot;&quot;).map{|c| c[0].to_i}.inject([]) {|v,s| v.push sprintf(&quot;%02X&quot;, s); v; }.values_at(3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12).join(&quot;&quot;)
=&gt; &quot;E2F32BD0F154704B0C176D1D02050303&quot;
</pre>
<p>我用 PHP 写了一个</p>
<pre class="brush: php">
&lt;?php
function nginx_userid_decode($str)
{
    $str_unpacked =  unpack(&#039;h*&#039;, base64_decode(str_replace(&#039; &#039;, &#039;+&#039;, $str)));
    $str_split = str_split(current($str_unpacked), 8);
    $str_map = array_map(&#039;strrev&#039;, $str_split);
    $str_dedoded = strtoupper(implode(&#039;&#039;, $str_map));

    return $str_dedoded;
}

// uid=8380A8C09A7E8C4B0A112CC202030303
echo nginx_userid_decode(&#039;wKiAg0uMfprCLBEKAwMDAg==&#039;);
</pre>
<p>update:</p>
<blockquote><p>
<strong>如果 base64 后的编码中含有 '+' , 在 url 传递中或是 $_COOKIE 数组读取中会被转换为空格</strong>
</p></blockquote>
<p>-- EOF --</p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>2010-05-21 -- <a href="http://www.lsproc.com/blog/nginx_php_pathinfo_securit/" title="Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo">Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo</a> (2)</li><li>2010-03-08 -- <a href="http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/" title="nginx+factcgi 下使用 ob_flush">nginx+factcgi 下使用 ob_flush</a> (0)</li><li>2009-03-29 -- <a href="http://www.lsproc.com/blog/nginx_ssl_config/" title="Nginx SSL 配置">Nginx SSL 配置</a> (22)</li><li>2008-10-23 -- <a href="http://www.lsproc.com/blog/nginx_rewrite_param/" title="nginx rewrite 的一些参数  ">nginx rewrite 的一些参数  </a> (1)</li><li>2008-06-15 -- <a href="http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/" title="lighttpd + PHP(fastcgi) 配置">lighttpd + PHP(fastcgi) 配置</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lsproc.com/blog/nginx_userid_decode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>nginx+factcgi 下使用 ob_flush</title>
		<link>http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/</link>
		<comments>http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 03:45:23 +0000</pubDate>
		<dc:creator>lostsnow</dc:creator>
				<category><![CDATA[Linux&Webserver]]></category>
		<category><![CDATA[buffer]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[ob_flush]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.lsproc.com/blog/?p=376</guid>
		<description><![CDATA[转载时请标明文章原始出处和作者信息, 作者: lostsnow.http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/
Nginx与php-cgi是两个独立的程序，通过TCP或Unix套接字通信，不像Apache那样是集成在一起的。所以，Nginx有fastcgi 缓冲区，数据超出缓冲区大小、或程序执行完，才会将内容输出到客户端。如果要使用ob_flush，不能开启gzip压缩输出。
nginx.conf:
fastcgi_buffer_size 4k;
fastcgi_buffers 8 4k;
gzip off;
php.ini:

output_buffering = Off


&#60;?php
set_time_limit(0);
ob_end_clean();
ob_implicit_flush(1);

for($i = 0; $i &#60; 10; $i++)
{
    echo $i . &#34;&#60;br /&#62;\n&#34;;
    echo str_repeat(&#039;&#039;, 1024*4);
    sleep(1);
}

其中 echo str_repeat('', 1024*4);
使得fastcgi_buffer_size 4k; 的缓冲区满，从而输出内容到浏览器
参考: http://blog.s135.com/nginx_php_v6/2/1/
-- EOF --
Related Posts2010-05-21 -- Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo (2)2010-04-12 -- nginx userid 模块客户端 cookie 解码 (0)2008-06-15 -- [...]]]></description>
			<content:encoded><![CDATA[<p>转载时请标明文章原始出处和作者信息, 作者: <a href="http://www.lsproc.com/blog/">lostsnow</a>.<br /><a href="http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/">http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/</a></p>
<p>Nginx与php-cgi是两个独立的程序，通过TCP或Unix套接字通信，不像Apache那样是集成在一起的。所以，Nginx有fastcgi 缓冲区，数据超出缓冲区大小、或程序执行完，才会将内容输出到客户端。<strong>如果要使用ob_flush，不能开启gzip压缩输出。</strong></p>
<p>nginx.conf:</p>
<pre class="brush: text">fastcgi_buffer_size 4k;
fastcgi_buffers 8 4k;
gzip off;</pre>
<p>php.ini:</p>
<pre class="brush: text">
output_buffering = Off
</pre>
<pre class="brush: php">
&lt;?php
set_time_limit(0);
ob_end_clean();
ob_implicit_flush(1);

for($i = 0; $i &lt; 10; $i++)
{
    echo $i . &quot;&lt;br /&gt;\n&quot;;
    echo str_repeat(&#039;&#039;, 1024*4);
    sleep(1);
}
</pre>
<p>其中 echo str_repeat('', 1024*4);<br />
使得fastcgi_buffer_size 4k; 的缓冲区满，从而输出内容到浏览器</p>
<p>参考: http://blog.s135.com/nginx_php_v6/2/1/
<p>-- EOF --</p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>2010-05-21 -- <a href="http://www.lsproc.com/blog/nginx_php_pathinfo_securit/" title="Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo">Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo</a> (2)</li><li>2010-04-12 -- <a href="http://www.lsproc.com/blog/nginx_userid_decode/" title="nginx userid 模块客户端 cookie 解码">nginx userid 模块客户端 cookie 解码</a> (0)</li><li>2008-06-15 -- <a href="http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/" title="lighttpd + PHP(fastcgi) 配置">lighttpd + PHP(fastcgi) 配置</a> (0)</li><li>2009-03-29 -- <a href="http://www.lsproc.com/blog/nginx_ssl_config/" title="Nginx SSL 配置">Nginx SSL 配置</a> (22)</li><li>2008-10-23 -- <a href="http://www.lsproc.com/blog/nginx_rewrite_param/" title="nginx rewrite 的一些参数  ">nginx rewrite 的一些参数  </a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>lighttpd + PHP(fastcgi) 配置</title>
		<link>http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/</link>
		<comments>http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/#comments</comments>
		<pubDate>Sun, 15 Jun 2008 11:06:58 +0000</pubDate>
		<dc:creator>lostsnow</dc:creator>
				<category><![CDATA[Linux&Webserver]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.lostk.com/blog/?p=149</guid>
		<description><![CDATA[转载时请标明文章原始出处和作者信息, 作者: lostsnow.http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/
php(fastcgi) 5.2.6 编译参数

./configure  --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-gd=/usr --enable-calendar --with-zlib --with-bz2 --with-curl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-openssl --enable-zend-multibyte --with-gettext --enable-exif --with-png-dir=/usr --with-jpeg-dir=/usr --with-libxml-dir=/usr --enable-gd-native-ttf --enable-dom --with-freetype-dir=/usr --with-iconv-dir=/usr --enable-fastcgi

lighttpd 1.5.0 编译参数

./configure --prefix=/usr/local/lighttpd --with-mysql=/usr/bin/mysql_config --with-openssl --with-pcre --with-bzip2 --enable-lfs --with-linux-aio

添加fastcgi 及lighttpd执行用户

groupadd www
useradd -g www -s /sbin/nologin -d /dev/null www

复制lighttpd源码包内 doc/spawn-php.sh 并修改如下

#!/bin/bash

## ABSOLUTE path to the spawn-fcgi binary
SPAWNFCGI=&#34;/usr/local/lighttpd/bin/spawn-fcgi&#34;

## ABSOLUTE path [...]]]></description>
			<content:encoded><![CDATA[<p>转载时请标明文章原始出处和作者信息, 作者: <a href="http://www.lsproc.com/blog/">lostsnow</a>.<br /><a href="http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/">http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/</a></p>
<p>php(fastcgi) 5.2.6 编译参数</p>
<pre class="brush: bash">
./configure  --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-gd=/usr --enable-calendar --with-zlib --with-bz2 --with-curl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-openssl --enable-zend-multibyte --with-gettext --enable-exif --with-png-dir=/usr --with-jpeg-dir=/usr --with-libxml-dir=/usr --enable-gd-native-ttf --enable-dom --with-freetype-dir=/usr --with-iconv-dir=/usr --enable-fastcgi
</pre>
<p>lighttpd 1.5.0 编译参数</p>
<pre class="brush: bash">
./configure --prefix=/usr/local/lighttpd --with-mysql=/usr/bin/mysql_config --with-openssl --with-pcre --with-bzip2 --enable-lfs --with-linux-aio
</pre>
<p>添加fastcgi 及lighttpd执行用户</p>
<pre class="brush: bash">
groupadd www
useradd -g www -s /sbin/nologin -d /dev/null www
</pre>
<p>复制lighttpd源码包内 doc/spawn-php.sh 并修改如下</p>
<pre class="brush: bash">
#!/bin/bash

## ABSOLUTE path to the spawn-fcgi binary
SPAWNFCGI=&quot;/usr/local/lighttpd/bin/spawn-fcgi&quot;

## ABSOLUTE path to the PHP binary
FCGIPROGRAM=&quot;/usr/local/php/bin/php-cgi&quot;

## TCP port to which to bind on localhost
FCGIPORT=&quot;1026&quot;

## SOCKET to which to bind on localhost
FCGISOCKET=&quot;/tmp/php-fastcgi1.sock&quot;

## number of PHP children to spawn
PHP_FCGI_CHILDREN=16

## maximum number of requests a single PHP process can serve before it is restarted
PHP_FCGI_MAX_REQUESTS=1000

## IP addresses from which PHP should access server connections
FCGI_WEB_SERVER_ADDRS=&quot;127.0.0.1&quot;

# allowed environment variables, separated by spaces
ALLOWED_ENV=&quot;HOME PATH USER&quot;

## if this script is run as root, switch to the following user
USERID=www
GROUPID=www

################## no config below this line

if test x$PHP_FCGI_CHILDREN = x; then
  PHP_FCGI_CHILDREN=5
fi

export PHP_FCGI_MAX_REQUESTS
export FCGI_WEB_SERVER_ADDRS

ALLOWED_ENV=&quot;$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS&quot;

# port
#if test x$UID = x0; then
#  EX=&quot;$SPAWNFCGI -p $FCGIPORT -f $FCGIPROGRAM -u $USERID -g $GROUPID -C $PHP_FCGI_CHILDREN&quot;
#else
#  EX=&quot;$SPAWNFCGI -p $FCGIPORT -f $FCGIPROGRAM -C $PHP_FCGI_CHILDREN&quot;
#fi

# socket
if test x$UID = x0; then
  EX=&quot;$SPAWNFCGI  -s $FCGISOCKET -f $FCGIPROGRAM -u $USERID -g $GROUPID -C $PHP_FCGI_CHILDREN&quot;
else
  EX=&quot;$SPAWNFCGI  -s $FCGISOCKET -f $FCGIPROGRAM -C $PHP_FCGI_CHILDREN&quot;
fi

# copy the allowed environment variables
E=

for i in $ALLOWED_ENV; do
  E=&quot;$E $i=${!i}&quot;
done

# clean the environment and set up a new one
env - $E $EX
</pre>
<p>保存为 spawn-php1.sh 放到 /etc/lighttpd 下, 多个fastcgi实例可复制此文件并修改相关端口号及socket</p>
<p>lighttpd 相关配置 lighttpd.conf 如下</p>
<pre class="brush: bash">
server.use-ipv6 = &quot;disable&quot; # 缺省为禁用
server.event-handler = &quot;linux-sysepoll&quot; # Linux环境下epoll系统调用可提高吞吐量
#server.max-worker = 10 # 如果你的系统资源没跑满，可考虑调高  lighttpd进程数
server.max-fds = 4096 # 默认的，应该够用了，可根据实际情况调整
server.max-connections = 4096 # 默认等于 server.max-fds
server.network-backend = &quot;linux-sendfile&quot;
server.max-keep-alive-requests = 0 # 在一个keep-alive会话终止连接前能接受处理的最大请求数。0为禁止

# 设置要加载的module
server.modules = (
    &quot;mod_rewrite&quot;,
    &quot;mod_redirect&quot;,
#    &quot;mod_alias&quot;,
    &quot;mod_access&quot;,
#    &quot;mod_cml&quot;,
#    &quot;mod_trigger_b4_dl&quot;,
    &quot;mod_auth&quot;,
    &quot;mod_expire&quot;,
#    &quot;mod_status&quot;,
#    &quot;mod_setenv&quot;,
    &quot;mod_proxy_core&quot;,
    &quot;mod_proxy_backend_http&quot;,
    &quot;mod_proxy_backend_fastcgi&quot;,
#    &quot;mod_proxy_backend_scgi&quot;,
#    &quot;mod_proxy_backend_ajp13&quot;,
#    &quot;mod_simple_vhost&quot;,
    &quot;mod_evhost&quot;,
#    &quot;mod_userdir&quot;,
#    &quot;mod_cgi&quot;,
    &quot;mod_compress&quot;,
#    &quot;mod_ssi&quot;,
#    &quot;mod_usertrack&quot;,
#    &quot;mod_secdownload&quot;,
#    &quot;mod_rrdtool&quot;,
    &quot;mod_accesslog&quot; )

# 网站根目录
server.document-root = &quot;/var/www/&quot;

# 错误日志位置
server.errorlog = &quot;/var/log/lighttpd/error.log&quot;

# 网站Index
index-file.names = ( &quot;index.php&quot;, &quot;index.html&quot;,
                                 &quot;index.htm&quot;, &quot;default.htm&quot; )

# 访问日志, 以及日志格式 (combined), 使用X-Forwarded-For可越过代理读取真实ip
accesslog.filename = &quot;/var/log/lighttpd/access.log&quot;
accesslog.format = &quot;%{X-Forwarded-For}i %v %u %t \&quot;%r\&quot; %s %b  \&quot;%{User-Agent}i\&quot; \&quot;%{Referer}i\&quot;&quot;

# 设置禁止访问的文件扩展名
url.access-deny = ( &quot;~&quot;, &quot;.inc&quot;, &quot;.tpl&quot; )

# 服务监听端口
server.port = 80

# 进程id记录位置
server.pid-file = &quot;/var/run/lighttpd.pid&quot;

# virtual directory listings 如果没有找到index文件就列出目录。建议disable。
dir-listing.activate = &quot;disable&quot;

# 服务运行使用的用户及用户组
server.username = &quot;www&quot;
server.groupname = &quot;www&quot;

# gzip压缩存放的目录以及需要压缩的文件类型
compress.cache-dir = &quot;/tmp/lighttpd/cache/compress/&quot;
compress.filetype = (&quot;text/plain&quot;, &quot;text/html&quot;)

# fastcgi module
# for PHP don&#039;t forget to set cgi.fix_pathinfo = 1 in the php.ini
$HTTP[&quot;url&quot;] =~ &quot;\.php$&quot; {
    proxy-core.balancer = &quot;round-robin&quot;
    proxy-core.allow-x-sendfile = &quot;enable&quot;
#    proxy-core.check-local = &quot;enable&quot;
    proxy-core.protocol = &quot;fastcgi&quot;
    proxy-core.backends = ( &quot;unix:/tmp/php-fastcgi1.sock&quot;,&quot;unix:/tmp/php-fastcgi2.sock&quot; )
    proxy-core.max-pool-size = 16
}

# 权限控制
auth.backend = &quot;htpasswd&quot;
auth.backend.htpasswd.userfile = &quot;/var/www/htpasswd.userfile&quot;

# 基于 evhost 的虚拟主机 针对域名
$HTTP[&quot;host&quot;] == &quot;a.lostk.com&quot; {
    server.document-root = &quot;/var/www/lostk/&quot;
    server.errorlog = &quot;/var/log/lighttpd/lostk-error.log&quot;
    accesslog.filename = &quot;/var/log/lighttpd/lostk-access.log&quot;

    # 设定文件过期时间
    expire.url = (
        &quot;/css/&quot; =&gt; &quot;access 2 hours&quot;,
        &quot;/js/&quot; =&gt; &quot;access 2 hours&quot;,
    )

    # url 跳转
    url.redirect = (
        &quot;^/$&quot; =&gt; &quot;/xxx/index.html&quot;,
    )

    # url 重写 (cakephp可用)
    url.rewrite = (
        &quot;^/(css|js)/(.*)$&quot; =&gt; &quot;/$1/$2&quot;,
        &quot;^/([^.]+)$&quot; =&gt; &quot;/index.php?url=$1&quot;,
    )

    # 权限控制
    auth.require   = ( &quot;&quot; =&gt;
         (
            &quot;method&quot; =&gt; &quot;basic&quot;,
            &quot;realm&quot; =&gt; &quot;admin only&quot;,
            &quot;require&quot; =&gt; &quot;user=admin1|user=admin2&quot;  # 允许的用户, 用户列表文件 在上面配置的auth.backend.htpasswd.userfile 里
        ),
    )
}

# 针对端口的虚拟主机
$SERVER[&quot;socket&quot;] == &quot;192.168.0.1:8000&quot; {
    server.document-root        = &quot;/var/www/xxx/&quot;
    server.errorlog = &quot;/var/log/lighttpd/test-error.log&quot;
    accesslog.filename = &quot;/var/log/lighttpd/test-access.log&quot;

   # ...
}
</pre>
<p># fastcgi 以及 lighttpd 启动脚本</p>
<pre class="brush: bash">
#!/bin/sh
startphp(){
    /etc/lighttpd/spawn-php1.sh
    /etc/lighttpd/spawn-php2.sh
}

starthttpd(){
    /usr/local/lighttpd/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
}

start(){
    startphp
    starthttpd
}

stopphp(){
    killall php-cgi
}

stophttpd(){
    killall lighttpd
}

stop(){
    killall php-cgi lighttpd
}

case &quot;$1&quot; in
    start)
        start
        ;;
    startphp)
        startphp
        ;;
    starthttpd)
        starthttpd
        ;;
    stop)
        stop
    ;;
    stopphp)
        stopphp
        ;;
    stophttpd)
        stophttpd
        ;;
    *)
        echo &quot;Usage: lighttpd {start|stop|startphp|starthttpd|stopphp|stophttpd}&quot;
        RETVAL=1
esac
</pre>
<p>-- EOF --</p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>2010-03-08 -- <a href="http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/" title="nginx+factcgi 下使用 ob_flush">nginx+factcgi 下使用 ob_flush</a> (0)</li><li>2010-05-21 -- <a href="http://www.lsproc.com/blog/nginx_php_pathinfo_securit/" title="Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo">Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo</a> (2)</li><li>2010-04-12 -- <a href="http://www.lsproc.com/blog/nginx_userid_decode/" title="nginx userid 模块客户端 cookie 解码">nginx userid 模块客户端 cookie 解码</a> (0)</li><li>2008-03-18 -- <a href="http://www.lsproc.com/blog/configure_lamp/" title="lamp 相关配置 [Debian]">lamp 相关配置 [Debian]</a> (2)</li><li>2008-01-17 -- <a href="http://www.lsproc.com/blog/use_dbprefix_in_zendframework/" title="ZendFramework 使用数据表前缀">ZendFramework 使用数据表前缀</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>lamp 相关配置 [Debian]</title>
		<link>http://www.lsproc.com/blog/configure_lamp/</link>
		<comments>http://www.lsproc.com/blog/configure_lamp/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 08:35:41 +0000</pubDate>
		<dc:creator>lostsnow</dc:creator>
				<category><![CDATA[Linux&Webserver]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.lostk.com/blog/configure_lamp/</guid>
		<description><![CDATA[转载时请标明文章原始出处和作者信息, 作者: lostsnow.http://www.lsproc.com/blog/configure_lamp/
编译环境
Debian (Ubuntu)

apt-get install build-essential
apt-get install libncurses5-dev
sudo apt-get install libxml2-dev libcurl3-dev libpng-dev libmhash-dev libmcrypt-dev libxslt-dev libpspell-dev

Mysql编译安装参数
CHOST=&#34;i686-pc-linux-gnu&#34; CFLAGS=&#34;-O3 -msse2 -mmmx -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer&#34; CXXFLAGS=&#34;-O3 -msse2 -mmmx -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer&#34; ./configure --prefix=/usr/local/mysql --localstatedir=/var/lib/mysql --with-comment=Source --with-server-suffix=-Community-Server --with-mysqld-user=mysql --without-debug --with-big-tables --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=all --with-pthread --enable-static --enable-thread-safe-client --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --without-ndb-debug --without-isam --with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock
配置成功会提示：
MySQL has [...]]]></description>
			<content:encoded><![CDATA[<p>转载时请标明文章原始出处和作者信息, 作者: <a href="http://www.lsproc.com/blog/">lostsnow</a>.<br /><a href="http://www.lsproc.com/blog/configure_lamp/">http://www.lsproc.com/blog/configure_lamp/</a></p>
<p>编译环境</p>
<p>Debian (Ubuntu)</p>
<pre class="brush: bash">
apt-get install build-essential
apt-get install libncurses5-dev
sudo apt-get install libxml2-dev libcurl3-dev libpng-dev libmhash-dev libmcrypt-dev libxslt-dev libpspell-dev
</pre>
<p><strong>Mysql编译安装参数</strong></p>
<pre class="brush: bash">CHOST=&quot;i686-pc-linux-gnu&quot; CFLAGS=&quot;-O3 -msse2 -mmmx -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer&quot; CXXFLAGS=&quot;-O3 -msse2 -mmmx -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer&quot; ./configure --prefix=/usr/local/mysql --localstatedir=/var/lib/mysql --with-comment=Source --with-server-suffix=-Community-Server --with-mysqld-user=mysql --without-debug --with-big-tables --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=all --with-pthread --enable-static --enable-thread-safe-client --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --without-ndb-debug --without-isam --with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock</pre>
<p>配置成功会提示：</p>
<p>MySQL has a Web site athttp://www.mysql.com/which carries details on the<br />
latest release, upcoming features, and other information to make your<br />
work or play with MySQL more productive. There you can also find<br />
information about mailing lists for MySQL discussion.</p>
<p>Remember to check the platform. specific part of the reference manual for<br />
hints about installing MySQL on your platform. Also have a look at the<br />
files in the Docs directory.</p>
<p>Thank you for choosing MySQL!</p>
<pre class="brush: bash">
make
make install

groupadd mysql                     //增加mysql组
useradd -g mysql mysql         //增加mysql用户，这个用户属于mysql组
cd /usr/local/mysql
bin/mysql_install_db --user=mysql
chown -R root:mysql . //设置权限，注意后面有一个 &quot;.&quot;
chown -R mysql /var/lib/mysql //设置 mysql 目录权限
chgrp -R mysql . //注意后面有一个 &quot;.&quot;
cp share/mysql/my-medium.cnf /etc/my.cnf
cp share/mysql/mysql.server /etc/init.d/mysqld //开机自动启动 mysql。
chmod 755 /etc/init.d/mysqld
rcconf    //开启启动服务设置
/etc/init.d/mysqld start //启动 MySQL
bin/mysqladmin -u root password &quot;password_for_root&quot;
</pre>
<p><strong>查看mysql编译参数</strong><br />
cat /usr/local/mysql/bin/mysqlbug |grep ./configure</p>
<p>把 mysql 加入环境变量<br />
export PATH="$PATH:/usr/local/mysql/bin"</p>
<p>apache 编译</p>
<pre class="brush: bash">
./configure   //配置源代码树
--prefix=/usr/local/apache2  //体系无关文件的顶级安装目录PREFIX ，也就Apache的安装目录。
--enable-module=so   //打开 so 模块，so 模块是用来提 DSO 支持的 apache 核心模块
--enable-deflate=shared   //支持网页压缩
--enable-expires=shared   //支持 HTTP 控制
--enable-rewrite=shared   //支持 URL 重写
--enable-cache  //支持缓存
--enable-file-cache  //支持文件缓存
--enable-mem-cache  //支持记忆缓存
--enable-disk-cache //支持磁盘缓存
--enable-static-support   //支持静态连接(默认为动态连接)
--enable-static-htpasswd   //使用静态连接编译 htpasswd - 管理用于基本认证的用户文件
--enable-static-htdigest  //使用静态连接编译 htdigest - 管理用于摘要认证的用户文件
--enable-static-rotatelogs  //使用静态连接编译 rotatelogs - 滚动 Apache 日志的管道日志程序
--enable-static-logresolve   //使用静态连接编译 logresolve - 解析 Apache 日志中的IP地址为主机名
--enable-static-htdbm   //使用静态连接编译 htdbm - 操作 DBM 密码数据库
--enable-static-ab  //使用静态连接编译 ab - Apache HTTP 服务器性能测试工具
--enable-static-checkgid   //使用静态连接编译 checkgid
--disable-cgid  //禁止用一个外部 CGI 守护进程执行CGI脚本
--disable-cgi   //禁止编译 CGI 版本的 PHP
--disable-userdir  //禁止用户从自己的主目录中提供页面
--with-mpm=worker // 让apache以worker方式运行
--enable-authn-dbm=shared // 对动态数据库进行操作。Rewrite时需要。

make
make install
</pre>
<p>建立一个符号连接：</p>
<pre class="brush: bash">
ln -s /usr/local/apache2/bin/apachectl /etc/init.d/httpd
rcconf    //加入自动启动
</pre>
<p>php 编译</p>
<pre class="brush: bash">
CHOST=&quot;i686-pc-linux-gnu&quot; CFLAGS=&quot;-O3 -msse2 -mmmx -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer&quot; CXXFLAGS=&quot;-O3 -msse2 -mmmx -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer&quot; ./configure --prefix=/usr/local/php5 --with-mysql=/usr/local/mysql --with-gd --enable-calendar --with-zlib --with-curl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-apxs2=/usr/local/apache2/bin/apxs --with-openssl --enable-zend-multibyte --with-gettext --with-mcrypt --enable-exif --with-png-dir=/usr/local/lib --enable-ftp --with-mhash --with-libxml-dir=/usr/local/lib --with-xsl --with-pspell
</pre>
<p>配置完成提示<br />
+--------------------------------------------------------------------+<br />
| License:                                                           |<br />
| This software is subject to the PHP License, available in this     |<br />
| distribution in the file LICENSE.  By continuing this installation |<br />
| process, you are bound by the terms of this license agreement.     |<br />
| If you do not agree with the terms of this license, you must abort |<br />
| the installation process at this point.                            |<br />
+--------------------------------------------------------------------+</p>
<p>Thank you for using PHP.</p>
<pre class="brush: bash">
make
make install
</pre>
<p>修改/usr/local/apache2/conf/httpd.conf，在AddType部分加入如下内容<br />
AddType application/x-httpd-php .php
<p>-- EOF --</p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>2006-08-22 -- <a href="http://www.lsproc.com/blog/trouble-with-rebuild-system/" title="重装系统是件麻烦的事">重装系统是件麻烦的事</a> (0)</li><li>2008-03-01 -- <a href="http://www.lsproc.com/blog/close_timewait_connection/" title="如何关闭time_wait连接">如何关闭time_wait连接</a> (4)</li><li>2007-09-07 -- <a href="http://www.lsproc.com/blog/php_mysql_study_4/" title="PHP 和 Mysql 学习笔记（四）">PHP 和 Mysql 学习笔记（四）</a> (0)</li><li>2007-06-22 -- <a href="http://www.lsproc.com/blog/php_mysql_study_2/" title="PHP 和 Mysql 学习笔记（二）">PHP 和 Mysql 学习笔记（二）</a> (0)</li><li>2010-05-21 -- <a href="http://www.lsproc.com/blog/nginx_php_pathinfo_securit/" title="Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo">Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo</a> (2)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lsproc.com/blog/configure_lamp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ZendFramework 使用数据表前缀</title>
		<link>http://www.lsproc.com/blog/use_dbprefix_in_zendframework/</link>
		<comments>http://www.lsproc.com/blog/use_dbprefix_in_zendframework/#comments</comments>
		<pubDate>Thu, 17 Jan 2008 15:07:15 +0000</pubDate>
		<dc:creator>lostsnow</dc:creator>
				<category><![CDATA[Program&Database]]></category>
		<category><![CDATA[dbprefix]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zendframework]]></category>

		<guid isPermaLink="false">http://www.lostk.com/blog/use_dbprefix_in_zendframework/</guid>
		<description><![CDATA[转载时请标明文章原始出处和作者信息, 作者: lostsnow.http://www.lsproc.com/blog/use_dbprefix_in_zendframework/
目录结构

在 config.ini 定义 prefix
[general]
adapter  = PDO_MYSQL
host     = localhost
username = root
password = 123456
dbname   = test
charset  = utf8
prefix   = pf_    //表前缀
index.php 中将 prefix 注册
// 读取数据库配置
$dbconfig = new Zend_Config_Ini(&#039;../config/config.ini&#039;, &#039;general&#039;);
// 配置数据库
$database = Zend_Db::factory($dbconfig-&#62;adapter,$dbconfig-&#62;toArray());
// 设置数据库编码
$database-&#62;query(&#34;set names {$dbconfig-&#62;charset};&#34;);
Zend_Db_Table::setDefaultAdapter($database);
Zend_Registry::set(&#039;database&#039;,$database);
// 数据表前缀
Zend_Registry::set(&#039;dbprefix&#039;,$dbconfig-&#62;prefix);
在 library/Custom 目录下新建文件 Db.php 继承 Zend_Db_Table 类
class [...]]]></description>
			<content:encoded><![CDATA[<p>转载时请标明文章原始出处和作者信息, 作者: <a href="http://www.lsproc.com/blog/">lostsnow</a>.<br /><a href="http://www.lsproc.com/blog/use_dbprefix_in_zendframework/">http://www.lsproc.com/blog/use_dbprefix_in_zendframework/</a></p>
<p>目录结构<br />
<img src="http://www.lsproc.com/blog/wp-content/uploads/2008/01/zfstruc.png" width="166" height="360" alt="zend framework dirs" title="zend framework dirs" /></p>
<p>在 config.ini 定义 prefix</p>
<pre class="brush: php">[general]
adapter  = PDO_MYSQL
host     = localhost
username = root
password = 123456
dbname   = test
charset  = utf8
prefix   = pf_    //表前缀</pre>
<p>index.php 中将 prefix 注册</p>
<pre class="brush: php">// 读取数据库配置
$dbconfig = new Zend_Config_Ini(&#039;../config/config.ini&#039;, &#039;general&#039;);
// 配置数据库
$database = Zend_Db::factory($dbconfig-&gt;adapter,$dbconfig-&gt;toArray());
// 设置数据库编码
$database-&gt;query(&quot;set names {$dbconfig-&gt;charset};&quot;);
Zend_Db_Table::setDefaultAdapter($database);
Zend_Registry::set(&#039;database&#039;,$database);
// 数据表前缀
Zend_Registry::set(&#039;dbprefix&#039;,$dbconfig-&gt;prefix);</pre>
<p>在 library/Custom 目录下新建文件 Db.php 继承 Zend_Db_Table 类</p>
<pre class="brush: php">class Custom_Db extends Zend_Db_Table
{
    public function __construct()
    {
        $dbprefix = Zend_Registry::get(&#039;dbprefix&#039;);
        $this-&gt;_name = $dbprefix.$this-&gt;_name;
        parent::__construct();
    }
}</pre>
<p>最后在 model 中继承 Custom_Db 即可</p>
<pre class="brush: php">class User extends Custom_Db
{
    protected $_name = &#039;users&#039;;     //在Custom_Db中会自动加上表名的前缀
    protected $_primary = &#039;userid&#039;; //主键
}</pre>
<p>-- EOF --</p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>2010-05-21 -- <a href="http://www.lsproc.com/blog/nginx_php_pathinfo_securit/" title="Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo">Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo</a> (2)</li><li>2010-04-12 -- <a href="http://www.lsproc.com/blog/nginx_userid_decode/" title="nginx userid 模块客户端 cookie 解码">nginx userid 模块客户端 cookie 解码</a> (0)</li><li>2010-03-08 -- <a href="http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/" title="nginx+factcgi 下使用 ob_flush">nginx+factcgi 下使用 ob_flush</a> (0)</li><li>2008-06-15 -- <a href="http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/" title="lighttpd + PHP(fastcgi) 配置">lighttpd + PHP(fastcgi) 配置</a> (0)</li><li>2008-03-18 -- <a href="http://www.lsproc.com/blog/configure_lamp/" title="lamp 相关配置 [Debian]">lamp 相关配置 [Debian]</a> (2)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lsproc.com/blog/use_dbprefix_in_zendframework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cakephp 笔记</title>
		<link>http://www.lsproc.com/blog/cakephp_note/</link>
		<comments>http://www.lsproc.com/blog/cakephp_note/#comments</comments>
		<pubDate>Mon, 12 Nov 2007 17:31:06 +0000</pubDate>
		<dc:creator>lostsnow</dc:creator>
				<category><![CDATA[Program&Database]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[笔记]]></category>

		<guid isPermaLink="false">http://localhost/lost/blog/webdesign/cakephp_note/</guid>
		<description><![CDATA[转载时请标明文章原始出处和作者信息, 作者: lostsnow.http://www.lsproc.com/blog/cakephp_note/
1. 自定义 layout
在 cakephp 中 layout 默认指向 /app/views/layouts/default.thtml
如果要自定义 layout，需要在 controller 中定义：
var $layout = 'mydefault';
layout 则指向了 /app/views/layouts/mydefault.thtml
2. 创建不使用数据库表的 model 或者改变 model 对应的表名
我需要创建一个不使用任何表的model。例如，我想通过 $validate 数组方便底验证输入数据，保持model逻辑的正确性。但创建 model 时对应的表不存在，CakePHP 就会报错。通过在 model 中加入以下代码可以解决这个问题：
var $useTable = false;
你也可以通过这种方法改变model对应的表名。
var $useTable = 'some_table';
3. 快速创建后台管理
如果你需要创建后台管理程序，并且希望所有管理action都位于某个特定文件夹下，那么打开 config/core.php 并将下面这一行的注释去掉：
define('CAKE_ADMIN', 'admin');
这样所有以"admin_"开头的action都可以通过 /admin/yourcontroller/youraction 来访问。例如，如果在 posts controller 中创建了名为 "admin_add" 的 action，那么可以通过 www.example.com/admin/posts/add 访问这个action。这样就可以方便地为 admin 目录设置密码以避免他人随意访问。
4. 自定义404页面
如果你需要自定义404页面，只需创建 /app/views/errors/error404.thtml。
5. 让controller使用其他model
如果你的controller需要调用来自不同model的数据，只要在controller开头使用如下代码：
class [...]]]></description>
			<content:encoded><![CDATA[<p>转载时请标明文章原始出处和作者信息, 作者: <a href="http://www.lsproc.com/blog/">lostsnow</a>.<br /><a href="http://www.lsproc.com/blog/cakephp_note/">http://www.lsproc.com/blog/cakephp_note/</a></p>
<p>1. 自定义 layout</p>
<p>在 cakephp 中 layout 默认指向 /app/views/layouts/default.thtml<br />
如果要自定义 layout，需要在 controller 中定义：<br />
<code>var $layout = 'mydefault';</code><br />
layout 则指向了 /app/views/layouts/mydefault.thtml</p>
<p>2. 创建不使用数据库表的 model 或者改变 model 对应的表名</p>
<p>我需要创建一个不使用任何表的model。例如，我想通过 $validate 数组方便底验证输入数据，保持model逻辑的正确性。但创建 model 时对应的表不存在，CakePHP 就会报错。通过在 model 中加入以下代码可以解决这个问题：</p>
<p><code>var $useTable = false;</code></p>
<p>你也可以通过这种方法改变model对应的表名。</p>
<p>var $useTable = 'some_table';</p>
<p>3. 快速创建后台管理</p>
<p>如果你需要创建后台管理程序，并且希望所有管理action都位于某个特定文件夹下，那么打开 config/core.php 并将下面这一行的注释去掉：</p>
<p><code>define('CAKE_ADMIN', 'admin');</code></p>
<p>这样所有以"admin_"开头的action都可以通过 /admin/yourcontroller/youraction 来访问。例如，如果在 posts controller 中创建了名为 "admin_add" 的 action，那么可以通过 www.example.com/admin/posts/add 访问这个action。这样就可以方便地为 admin 目录设置密码以避免他人随意访问。</p>
<p>4. 自定义404页面</p>
<p>如果你需要自定义404页面，只需创建 /app/views/errors/error404.thtml。</p>
<p>5. 让controller使用其他model</p>
<p>如果你的controller需要调用来自不同model的数据，只要在controller开头使用如下代码：</p>
<p><code>class yourController extends AppController {<br />
var $uses = array('Post','User');<br />
}</code></p>
<p>这样controller就能访问Post和User model了。
<p>-- EOF --</p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>2007-11-13 -- <a href="http://www.lsproc.com/blog/cakephp_callback_function/" title="Cakephp 的回调函数">Cakephp 的回调函数</a> (0)</li><li>2007-11-09 -- <a href="http://www.lsproc.com/blog/zend_framework_summary/" title="Zend 框架摘要">Zend 框架摘要</a> (0)</li><li>2007-10-31 -- <a href="http://www.lsproc.com/blog/cakephp_conventions/" title="CakePHP 命名规则">CakePHP 命名规则</a> (1)</li><li>2007-09-07 -- <a href="http://www.lsproc.com/blog/php_mysql_study_4/" title="PHP 和 Mysql 学习笔记（四）">PHP 和 Mysql 学习笔记（四）</a> (0)</li><li>2007-06-22 -- <a href="http://www.lsproc.com/blog/php_mysql_study_2/" title="PHP 和 Mysql 学习笔记（二）">PHP 和 Mysql 学习笔记（二）</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lsproc.com/blog/cakephp_note/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cakephp 的回调函数</title>
		<link>http://www.lsproc.com/blog/cakephp_callback_function/</link>
		<comments>http://www.lsproc.com/blog/cakephp_callback_function/#comments</comments>
		<pubDate>Mon, 12 Nov 2007 17:20:05 +0000</pubDate>
		<dc:creator>lostsnow</dc:creator>
				<category><![CDATA[Program&Database]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://localhost/lost/blog/webdesign/cakephp_callback_function/</guid>
		<description><![CDATA[转载时请标明文章原始出处和作者信息, 作者: lostsnow.http://www.lsproc.com/blog/cakephp_callback_function/
Model的Callbacks
我们在model中增加了一些回调函数以帮助你在model操作前后能够织入一些业务逻辑（原文为sneak in，借用了AOP中的织入一词，因为从操作来看这些回调函数等同于AOP中的advice）。为了获得这样的能力，需要使用model中的一些参数并且在你的model中覆写这些方法。
beforeFind(string $conditions)
beforeFind()回调函数是在model的find方法执行前的前置操作。你可以加入任何检索前的业务逻辑。你覆写该方法只要保证在前置操作成功后返回true来执行真正的find方法，返回false中断find方法就可以了。（译注：在一些复杂场景中，需多次持久化的情况下请慎用）。
afterFind(array $results)
使用afterFind回调函数能够更改find方法的返回结果集，或者在检索动作完成后加上一些业务逻辑。该函数的参数$results为经过回调函数处理以后的find检索结果集。
beforeValidate()
beforeValidate回调函数能够在model校验数据之前更改model中的一些数据。同样也可以用来在model校验之前加入更为复杂的额外校验规则。和beforeFind一样，必须保证返回true来调用真正的操作，返回false来中断校验乃至save操作。
beforeSave()
和先前介绍的回调函数类似，在校验完成之后，保存动作之前加入额外的处理（如果校验失败是不会触发该回调函数的）。返回true或者false，不再赘述。
一个比较常见的beforeSave的应用场景就是在保存动作之前格式化日期属性以适应不同的数据库：
// Date/time fields created by HTML Helper:
// This code would be seen in a view 

$html-&#62;dayOptionTag(&#039;Event/start&#039;);
$html-&#62;monthOptionTag(&#039;Event/start&#039;);
$html-&#62;yearOptionTag(&#039;Event/start&#039;);
$html-&#62;hourOptionTag(&#039;Event/start&#039;);
$html-&#62;minuteOptionTag(&#039;Event/start&#039;); 

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/ 

// Model callback functions used to stitch date
// data together for storage
// This code would be seen in the Event model: 

function beforeSave()
{
    $this-&#62;data[&#039;Event&#039;][&#039;start&#039;] = $this-&#62;_getDate(&#039;Event&#039;, &#039;start&#039;); 

   [...]]]></description>
			<content:encoded><![CDATA[<p>转载时请标明文章原始出处和作者信息, 作者: <a href="http://www.lsproc.com/blog/">lostsnow</a>.<br /><a href="http://www.lsproc.com/blog/cakephp_callback_function/">http://www.lsproc.com/blog/cakephp_callback_function/</a></p>
<p><strong>Model的Callbacks</strong></p>
<p>我们在model中增加了一些回调函数以帮助你在model操作前后能够织入一些业务逻辑（原文为sneak in，借用了AOP中的织入一词，因为从操作来看这些回调函数等同于AOP中的advice）。为了获得这样的能力，需要使用model中的一些参数并且在你的model中覆写这些方法。</p>
<p>beforeFind(string $conditions)</p>
<p>beforeFind()回调函数是在model的find方法执行前的前置操作。你可以加入任何检索前的业务逻辑。你覆写该方法只要保证在前置操作成功后返回true来执行真正的find方法，返回false中断find方法就可以了。（译注：在一些复杂场景中，需多次持久化的情况下请慎用）。</p>
<p>afterFind(array $results)</p>
<p>使用afterFind回调函数能够更改find方法的返回结果集，或者在检索动作完成后加上一些业务逻辑。该函数的参数$results为经过回调函数处理以后的find检索结果集。</p>
<p>beforeValidate()<br />
beforeValidate回调函数能够在model校验数据之前更改model中的一些数据。同样也可以用来在model校验之前加入更为复杂的额外校验规则。和beforeFind一样，必须保证返回true来调用真正的操作，返回false来中断校验乃至save操作。</p>
<p>beforeSave()</p>
<p>和先前介绍的回调函数类似，在校验完成之后，保存动作之前加入额外的处理（如果校验失败是不会触发该回调函数的）。返回true或者false，不再赘述。<br />
一个比较常见的beforeSave的应用场景就是在保存动作之前格式化日期属性以适应不同的数据库：</p>
<pre class="brush: php">// Date/time fields created by HTML Helper:
// This code would be seen in a view 

$html-&gt;dayOptionTag(&#039;Event/start&#039;);
$html-&gt;monthOptionTag(&#039;Event/start&#039;);
$html-&gt;yearOptionTag(&#039;Event/start&#039;);
$html-&gt;hourOptionTag(&#039;Event/start&#039;);
$html-&gt;minuteOptionTag(&#039;Event/start&#039;); 

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/ 

// Model callback functions used to stitch date
// data together for storage
// This code would be seen in the Event model: 

function beforeSave()
{
    $this-&gt;data[&#039;Event&#039;][&#039;start&#039;] = $this-&gt;_getDate(&#039;Event&#039;, &#039;start&#039;); 

    return true;
} 

function _getDate($model, $field)
{
    return date(&#039;Y-m-d H:i:s&#039;, mktime(
        intval($this-&gt;data[$model][$field . &#039;_hour&#039;]),
        intval($this-&gt;data[$model][$field . &#039;_min&#039;]),
        null,
        intval($this-&gt;data[$model][$field . &#039;_month&#039;]),
        intval($this-&gt;data[$model][$field . &#039;_day&#039;]),
        intval($this-&gt;data[$model][$field . &#039;_year&#039;])));
}
</pre>
<p>afterSave()</p>
<p>放置任何你想要在保存后执行的代码在这个回调函数中。[ToDo 如果保存出错是否会触发？]</p>
<p>beforeDelete()</p>
<p>放置删除前的逻辑代码。想要删除操作执行则返回true，否则返回false。</p>
<p>afterDelete()</p>
<p>放置任何你想要在删除后执行的代码在这个回调函数中。</p>
<p><strong>controller中的回调函数</strong></p>
<p>Cake的controller特别提供了许多回调方法，你可以用它们在一些重要的controller方法之前或者之后插入一些逻辑。如果要利用这些功能，请在你的controller中用这里描述的参数和返回值定义这些方法。</p>
<p>beforeFilter</p>
<p>在每个controller action调用前执行。它是一个检查当前sessoin状态和检查用户角色的好地方。</p>
<p>afterFilter</p>
<p>在每个controller action调用后执行。</p>
<p>beforeRender</p>
<p>在controller逻辑之后，并且在输出视图之前被调用。
<p>-- EOF --</p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>2007-11-13 -- <a href="http://www.lsproc.com/blog/cakephp_note/" title="Cakephp 笔记">Cakephp 笔记</a> (0)</li><li>2007-10-31 -- <a href="http://www.lsproc.com/blog/cakephp_conventions/" title="CakePHP 命名规则">CakePHP 命名规则</a> (1)</li><li>2010-05-21 -- <a href="http://www.lsproc.com/blog/nginx_php_pathinfo_securit/" title="Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo">Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo</a> (2)</li><li>2010-04-12 -- <a href="http://www.lsproc.com/blog/nginx_userid_decode/" title="nginx userid 模块客户端 cookie 解码">nginx userid 模块客户端 cookie 解码</a> (0)</li><li>2010-03-08 -- <a href="http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/" title="nginx+factcgi 下使用 ob_flush">nginx+factcgi 下使用 ob_flush</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lsproc.com/blog/cakephp_callback_function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend 框架摘要</title>
		<link>http://www.lsproc.com/blog/zend_framework_summary/</link>
		<comments>http://www.lsproc.com/blog/zend_framework_summary/#comments</comments>
		<pubDate>Thu, 08 Nov 2007 17:31:03 +0000</pubDate>
		<dc:creator>lostsnow</dc:creator>
				<category><![CDATA[Program&Database]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://localhost/lost/blog/webdesign/zend_framework_summary/</guid>
		<description><![CDATA[转载时请标明文章原始出处和作者信息, 作者: lostsnow.http://www.lsproc.com/blog/zend_framework_summary/
From: http://www.ibm.com/developerworks/cn/opensource/top-projects/php-resources.html#zend
Zend 框架组件包括：
Zend_Controller
此模块为应用程序提供全面的控制。它将请求转化为特定的行为并确保其执行。
Zend_Db
此模块基于 PHP 数据对象 (PDO) 并提供一种通用方式来访问数据库。
Zend_Feed
此模块使使用 RSS 和 Atom 提要变得简单。
Zend_Filter
此模块提供字符串过滤函数，如 isEmail() 和 getAlpha()。
Zend_InputFilter
对于 Zend_Filter，此模块是用来操作数组的，如表单输入。
Zend_HttpClient
此模块使您能轻易地执行 HTTP 请求。
Zend_Json
此模块使您能够轻易地将 PHP 对象转换成 JavaScript 对象符号，反之亦然。
Zend_Log
此模块提供通用日志功能。
Zend_Mail
此模块使您能够发送文本文件和多部分 MIME 电子邮件。
Zend_Mime
此模块被 Zend_Mail 用来解码 MIME 消息。
Zend_Pdf
此模块用来创建新的 PDF 文档，及加载和编辑现有文档。
Zend_Search
此模块使您能在现有文本上执行复杂搜索。例如，您可以建立一个搜索引擎，该引擎可以基于相关性或其他因素返回结果。
Zend_Service_Amazon、Zend_Service_Flickr 以及 Zend_Service_Yahoo
这些模块提供对这些 Web 服务 API 的简单访问。
Zend_View
此模块处理 MVC 模式的 “视图” 部分。
Zend_XmlRpc
此模块使您能够轻易地创建 XML-RPC 客户机。（已为将来计划好服务器功能。）
编码准则
当您为 Zend 框架编码，或使用其编码时，您应该遵循特定的准则。这些准则是为了使团队项目变得更加简单而设计的。换句话说，通过定义编码规范，您不仅能避免接下来的问题，而且能够使其他人更容易阅读您的代码。Zend 框架文档中包含几页指导准则，比如：
    * 确保文件整洁。换句话说，在文件头、标准的四格缩进等前面不能有任何前导或者后置的空格，这些空格会调用 Web 服务器导致意外地发送内容。
   [...]]]></description>
			<content:encoded><![CDATA[<p>转载时请标明文章原始出处和作者信息, 作者: <a href="http://www.lsproc.com/blog/">lostsnow</a>.<br /><a href="http://www.lsproc.com/blog/zend_framework_summary/">http://www.lsproc.com/blog/zend_framework_summary/</a></p>
<p>From: <a href="http://www.ibm.com/developerworks/cn/opensource/top-projects/php-resources.html#zend">http://www.ibm.com/developerworks/cn/opensource/top-projects/php-resources.html#zend</a></p>
<p>Zend 框架组件包括：</p>
<p>Zend_Controller<br />
此模块为应用程序提供全面的控制。它将请求转化为特定的行为并确保其执行。</p>
<p>Zend_Db<br />
此模块基于 PHP 数据对象 (PDO) 并提供一种通用方式来访问数据库。</p>
<p>Zend_Feed<br />
此模块使使用 RSS 和 Atom 提要变得简单。</p>
<p>Zend_Filter<br />
此模块提供字符串过滤函数，如 isEmail() 和 getAlpha()。</p>
<p>Zend_InputFilter<br />
对于 Zend_Filter，此模块是用来操作数组的，如表单输入。</p>
<p>Zend_HttpClient<br />
此模块使您能轻易地执行 HTTP 请求。</p>
<p>Zend_Json<br />
此模块使您能够轻易地将 PHP 对象转换成 JavaScript 对象符号，反之亦然。</p>
<p>Zend_Log<br />
此模块提供通用日志功能。</p>
<p>Zend_Mail<br />
此模块使您能够发送文本文件和多部分 MIME 电子邮件。</p>
<p>Zend_Mime<br />
此模块被 Zend_Mail 用来解码 MIME 消息。</p>
<p>Zend_Pdf<br />
此模块用来创建新的 PDF 文档，及加载和编辑现有文档。</p>
<p>Zend_Search<br />
此模块使您能在现有文本上执行复杂搜索。例如，您可以建立一个搜索引擎，该引擎可以基于相关性或其他因素返回结果。</p>
<p>Zend_Service_Amazon、Zend_Service_Flickr 以及 Zend_Service_Yahoo<br />
这些模块提供对这些 Web 服务 API 的简单访问。</p>
<p>Zend_View<br />
此模块处理 MVC 模式的 “视图” 部分。</p>
<p>Zend_XmlRpc<br />
此模块使您能够轻易地创建 XML-RPC 客户机。（已为将来计划好服务器功能。）</p>
<p>编码准则</p>
<p>当您为 Zend 框架编码，或使用其编码时，您应该遵循特定的准则。这些准则是为了使团队项目变得更加简单而设计的。换句话说，通过定义编码规范，您不仅能避免接下来的问题，而且能够使其他人更容易阅读您的代码。Zend 框架文档中包含几页指导准则，比如：</p>
<p>    * 确保文件整洁。换句话说，在文件头、标准的四格缩进等前面不能有任何前导或者后置的空格，这些空格会调用 Web 服务器导致意外地发送内容。<br />
    * 有且只有当类作为 Zend 框架的一部分时，而不仅仅是使用 Zend 框架时，类名才以 Zend_ 开头。<br />
    * 在函数名中下划线是禁止的。而是使用小写开头大小写间隔的方式（如 getTodaysDate()）。<br />
    * 只有当变量是 private 或 protected 类型时，才以下划线开头。<br />
    * 将所有的变量声明为 private、protected 或者 public，而不要使用 var。<br />
    * 使用标准的 PHP 标记（如 ），而不是简写方式 ()。<br />
    * 确保您的代码易于阅读。换句话说，当使用一个句号 (.) 连接文本时，确保在句号前后加上空格以便于阅读。同理，当声明一个数组时，要在逗号后面加上空格。<br />
    * 如果您必须通过引用传值，那只能在函数声明中这样做。调用时通过引用传值是不允许的。<br />
    * 任何 PHP 文件都必须包括能被 PhpDocumentor 阅读的文档，并且编码准则指定了特定的最少标记。
<p>-- EOF --</p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>2007-11-13 -- <a href="http://www.lsproc.com/blog/cakephp_note/" title="Cakephp 笔记">Cakephp 笔记</a> (0)</li><li>2010-05-21 -- <a href="http://www.lsproc.com/blog/nginx_php_pathinfo_securit/" title="Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo">Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo</a> (2)</li><li>2010-04-12 -- <a href="http://www.lsproc.com/blog/nginx_userid_decode/" title="nginx userid 模块客户端 cookie 解码">nginx userid 模块客户端 cookie 解码</a> (0)</li><li>2010-03-08 -- <a href="http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/" title="nginx+factcgi 下使用 ob_flush">nginx+factcgi 下使用 ob_flush</a> (0)</li><li>2008-06-15 -- <a href="http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/" title="lighttpd + PHP(fastcgi) 配置">lighttpd + PHP(fastcgi) 配置</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lsproc.com/blog/zend_framework_summary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>生成随机汉字字符串 (UTF8)</title>
		<link>http://www.lsproc.com/blog/get_randnum_chinese_string/</link>
		<comments>http://www.lsproc.com/blog/get_randnum_chinese_string/#comments</comments>
		<pubDate>Wed, 07 Nov 2007 17:26:43 +0000</pubDate>
		<dc:creator>lostsnow</dc:creator>
				<category><![CDATA[Program&Database]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[字符串]]></category>
		<category><![CDATA[汉字]]></category>

		<guid isPermaLink="false">http://localhost/lost/blog/webdesign/get_randnum_chinese_string/</guid>
		<description><![CDATA[转载时请标明文章原始出处和作者信息, 作者: lostsnow.http://www.lsproc.com/blog/get_randnum_chinese_string/
&#60;?php
$outputstr = getRandNumChineseString();
echo $outputstr;
function getChineseCharacter()
{
$unidec = rand(19968, 40869);
$unichr = &#039;&#38;#&#039; . $unidec . &#039;;&#039;;
$zhcnchr = mb_convert_encoding($unichr, &#34;UTF-8&#34;, &#34;HTML-ENTITIES&#34;);
return $zhcnchr;
}

function getRandNumChineseString()
{
$num = rand(1,16);
$str = null;
for($i=0;$i&#60;$num;$i++)
{
$str = $str . getChineseCharacter();
}
return $str;
}
?&#62;
PHP 需要开启 mbstring 支持
附: 汉字Unicode编码表
-- EOF --
Related Posts2006-03-01 -- 汉字截取的方法 (2)2010-05-21 -- Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo (2)2010-04-12 -- nginx userid 模块客户端 cookie 解码 (0)2010-03-08 -- nginx+factcgi 下使用 [...]]]></description>
			<content:encoded><![CDATA[<p>转载时请标明文章原始出处和作者信息, 作者: <a href="http://www.lsproc.com/blog/">lostsnow</a>.<br /><a href="http://www.lsproc.com/blog/get_randnum_chinese_string/">http://www.lsproc.com/blog/get_randnum_chinese_string/</a></p>
<pre class="brush: php">&lt;?php
$outputstr = getRandNumChineseString();
echo $outputstr;
function getChineseCharacter()
{
$unidec = rand(19968, 40869);
$unichr = &#039;&amp;#&#039; . $unidec . &#039;;&#039;;
$zhcnchr = mb_convert_encoding($unichr, &quot;UTF-8&quot;, &quot;HTML-ENTITIES&quot;);
return $zhcnchr;
}

function getRandNumChineseString()
{
$num = rand(1,16);
$str = null;
for($i=0;$i&lt;$num;$i++)
{
$str = $str . getChineseCharacter();
}
return $str;
}
?&gt;</pre>
<p>PHP 需要开启 mbstring 支持<br />
附: <a href="http://www.chi2ko.com/tool/CJK.htm">汉字Unicode编码表</a>
<p>-- EOF --</p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>2006-03-01 -- <a href="http://www.lsproc.com/blog/intercept-chinese-char/" title="汉字截取的方法">汉字截取的方法</a> (2)</li><li>2010-05-21 -- <a href="http://www.lsproc.com/blog/nginx_php_pathinfo_securit/" title="Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo">Nginx/PHP 文件类型错误解析漏洞：fix_pathinfo</a> (2)</li><li>2010-04-12 -- <a href="http://www.lsproc.com/blog/nginx_userid_decode/" title="nginx userid 模块客户端 cookie 解码">nginx userid 模块客户端 cookie 解码</a> (0)</li><li>2010-03-08 -- <a href="http://www.lsproc.com/blog/use_ob_flush_on_nginx_fastcgi/" title="nginx+factcgi 下使用 ob_flush">nginx+factcgi 下使用 ob_flush</a> (0)</li><li>2008-06-15 -- <a href="http://www.lsproc.com/blog/lighttpd_phpfastcgi_config/" title="lighttpd + PHP(fastcgi) 配置">lighttpd + PHP(fastcgi) 配置</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.lsproc.com/blog/get_randnum_chinese_string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
