<?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>idea&#039;s blog &#187; Web</title>
	<atom:link href="http://www.ideawu.net/blog/category/web/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ideawu.net/blog</link>
	<description>网络服务器架构, Linux C/C++服务器端开发, TCP/IP网络协议, PHP Web后端和Web前端开发, 网站架构.</description>
	<lastBuildDate>Thu, 12 Jan 2012 14:55:11 +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>PHP重用curl句柄, CURLOPT_HTTPGET的BUG</title>
		<link>http://www.ideawu.net/blog/archives/622.html</link>
		<comments>http://www.ideawu.net/blog/archives/622.html#comments</comments>
		<pubDate>Wed, 12 Oct 2011 10:55:51 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/2011/10/php%e9%87%8d%e7%94%a8curl%e5%8f%a5%e6%9f%84-curlopt_httpget%e7%9a%84bug.html</guid>
		<description><![CDATA[<p>重用一个CURL句柄时, 发现curl_setopt($ch, CURLOPT_HTTPGET, TRUE) 不起作用. 期望在调用这条语句之后发起请求, 应该发送的是GET, 但看服务器log, 却使用了和前一次请求相同的HTTP方法.</p>
<p>PHP脚本:</p>
<pre>
&lt;?php
$url = 'http://www.ideawu.net/';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_exec($ch);

curl_setopt($ch, CURLOPT_HTTPGET, true); // 错误! BUG
curl_exec($ch);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); // 正确
curl_exec($ch);
?&gt;
</pre>
<p>web server log:</p>
<pre>
124.127.130.50 "2011-10-12 18:55:09" "POST / HTTP/1.1" 200 3516 "-" www.ideawu.net
124.127.130.50 "2011-10-12 18:55:09" "POST / HTTP/1.1" 200 3516 "-" www.ideawu.net
124.127.130.50 "2011-10-12 18:55:09" "GET / HTTP/1.1" 200 3516 "-" www.ideawu.net
</pre>
<p>这个BUG目前还没找到相关的资料.</p>
<p><b>补充: 不仅仅是CURLOPT_HTTPGET, CURLOPT_POST也有同样的问题. 所以, 结论是: 只有CURLOPT_CUSTOMREQUEST才是正确的方法.</b></p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/517.html' rel='bookmark' title='Permanent Link: HTTP POST using PHP cURL'>HTTP POST using PHP cURL</a></li>
<li><a href='http://www.ideawu.net/blog/archives/556.html' rel='bookmark' title='Permanent Link: lighttpd配置HTTPS(SSL)'>lighttpd配置HTTPS(SSL)</a></li>
<li><a href='http://www.ideawu.net/blog/archives/114.html' rel='bookmark' title='Permanent Link: 艾薇儿Avril Lavigne的照片'>艾薇儿Avril Lavigne的照片</a></li>
<li><a href='http://www.ideawu.net/blog/archives/410.html' rel='bookmark' title='Permanent Link: PHP字符串==比较的副作用'>PHP字符串==比较的副作用</a></li>
<li><a href='http://www.ideawu.net/blog/archives/376.html' rel='bookmark' title='Permanent Link: 使用i_am_not_spam Wordpress插件'>使用i_am_not_spam Wordpress插件</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/622.html" title="PHP重用curl句柄, CURLOPT_HTTPGET的BUG">PHP重用curl句柄, CURLOPT_HTTPGET的BUG</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>重用一个CURL句柄时, 发现curl_setopt($ch, CURLOPT_HTTPGET, TRUE) 不起作用. 期望在调用这条语句之后发起请求, 应该发送的是GET, 但看服务器log, 却使用了和前一次请求相同的HTTP方法.</p>
<p>PHP脚本:</p>
<pre>
&lt;?php
$url = 'http://www.ideawu.net/';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_exec($ch);

curl_setopt($ch, CURLOPT_HTTPGET, true); // 错误! BUG
curl_exec($ch);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); // 正确
curl_exec($ch);
?&gt;
</pre>
<p>web server log:</p>
<pre>
124.127.130.50 "2011-10-12 18:55:09" "POST / HTTP/1.1" 200 3516 "-" www.ideawu.net
124.127.130.50 "2011-10-12 18:55:09" "POST / HTTP/1.1" 200 3516 "-" www.ideawu.net
124.127.130.50 "2011-10-12 18:55:09" "GET / HTTP/1.1" 200 3516 "-" www.ideawu.net
</pre>
<p>这个BUG目前还没找到相关的资料.</p>
<p><b>补充: 不仅仅是CURLOPT_HTTPGET, CURLOPT_POST也有同样的问题. 所以, 结论是: 只有CURLOPT_CUSTOMREQUEST才是正确的方法.</b></p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/517.html' rel='bookmark' title='Permanent Link: HTTP POST using PHP cURL'>HTTP POST using PHP cURL</a></li>
<li><a href='http://www.ideawu.net/blog/archives/556.html' rel='bookmark' title='Permanent Link: lighttpd配置HTTPS(SSL)'>lighttpd配置HTTPS(SSL)</a></li>
<li><a href='http://www.ideawu.net/blog/archives/114.html' rel='bookmark' title='Permanent Link: 艾薇儿Avril Lavigne的照片'>艾薇儿Avril Lavigne的照片</a></li>
<li><a href='http://www.ideawu.net/blog/archives/410.html' rel='bookmark' title='Permanent Link: PHP字符串==比较的副作用'>PHP字符串==比较的副作用</a></li>
<li><a href='http://www.ideawu.net/blog/archives/376.html' rel='bookmark' title='Permanent Link: 使用i_am_not_spam Wordpress插件'>使用i_am_not_spam Wordpress插件</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/622.html" title="PHP重用curl句柄, CURLOPT_HTTPGET的BUG">PHP重用curl句柄, CURLOPT_HTTPGET的BUG</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/622.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP浮点数显示和转成字符串</title>
		<link>http://www.ideawu.net/blog/archives/619.html</link>
		<comments>http://www.ideawu.net/blog/archives/619.html#comments</comments>
		<pubDate>Fri, 09 Sep 2011 03:33:47 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/2011/09/php%e6%b5%ae%e7%82%b9%e6%95%b0%e6%98%be%e7%a4%ba%e5%92%8c%e8%bd%ac%e6%88%90%e5%ad%97%e7%ac%a6%e4%b8%b2.html</guid>
		<description><![CDATA[<p>你可能会觉得PHP中将浮点数(float)转成字符串非常简单, 但是, 常用的方法隐藏着严重的bug. 因为, PHP在处理浮点数时有非常不合理的做法, 会有精度丢失. 经研究, 其实是PHP在显示浮点数时的问题, 也可以说是BUG. PHP内置的echo, var_dump, json_encode, 字符串拼接等函数(指令)在显示浮点数时都有问题, 导致精度丢失.</p>
<pre>
&lt;?php
$a = 1315537636.338467;
printf("%f", $a); echo "\n";
echo $a . "\n";
echo $a; echo "\n";
</pre>
<p>结果</p>
<pre>
1315537636.338467
1315537636.3385
1315537636.3385
</pre>
<p>也就是说, 用PHP最顺手的方法将浮点数转成字符串或者显示是不行的, 必须使用printf/sprintf将浮点数转成字符串.</p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/518.html' rel='bookmark' title='Permanent Link: if-else对优化代码冗余度的反作用'>if-else对优化代码冗余度的反作用</a></li>
<li><a href='http://www.ideawu.net/blog/archives/468.html' rel='bookmark' title='Permanent Link: 后端开发工程师的DIV+CSS两栏布局入门'>后端开发工程师的DIV+CSS两栏布局入门</a></li>
<li><a href='http://www.ideawu.net/blog/archives/367.html' rel='bookmark' title='Permanent Link: Wordpress分页代码'>Wordpress分页代码</a></li>
<li><a href='http://www.ideawu.net/blog/archives/582.html' rel='bookmark' title='Permanent Link: PHP的continue 2'>PHP的continue 2</a></li>
<li><a href='http://www.ideawu.net/blog/archives/464.html' rel='bookmark' title='Permanent Link: 用PHP去除重复图片文件'>用PHP去除重复图片文件</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/619.html" title="PHP浮点数显示和转成字符串">PHP浮点数显示和转成字符串</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>你可能会觉得PHP中将浮点数(float)转成字符串非常简单, 但是, 常用的方法隐藏着严重的bug. 因为, PHP在处理浮点数时有非常不合理的做法, 会有精度丢失. 经研究, 其实是PHP在显示浮点数时的问题, 也可以说是BUG. PHP内置的echo, var_dump, json_encode, 字符串拼接等函数(指令)在显示浮点数时都有问题, 导致精度丢失.</p>
<pre>
&lt;?php
$a = 1315537636.338467;
printf("%f", $a); echo "\n";
echo $a . "\n";
echo $a; echo "\n";
</pre>
<p>结果</p>
<pre>
1315537636.338467
1315537636.3385
1315537636.3385
</pre>
<p>也就是说, 用PHP最顺手的方法将浮点数转成字符串或者显示是不行的, 必须使用printf/sprintf将浮点数转成字符串.</p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/518.html' rel='bookmark' title='Permanent Link: if-else对优化代码冗余度的反作用'>if-else对优化代码冗余度的反作用</a></li>
<li><a href='http://www.ideawu.net/blog/archives/468.html' rel='bookmark' title='Permanent Link: 后端开发工程师的DIV+CSS两栏布局入门'>后端开发工程师的DIV+CSS两栏布局入门</a></li>
<li><a href='http://www.ideawu.net/blog/archives/367.html' rel='bookmark' title='Permanent Link: Wordpress分页代码'>Wordpress分页代码</a></li>
<li><a href='http://www.ideawu.net/blog/archives/582.html' rel='bookmark' title='Permanent Link: PHP的continue 2'>PHP的continue 2</a></li>
<li><a href='http://www.ideawu.net/blog/archives/464.html' rel='bookmark' title='Permanent Link: 用PHP去除重复图片文件'>用PHP去除重复图片文件</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/619.html" title="PHP浮点数显示和转成字符串">PHP浮点数显示和转成字符串</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/619.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache用mod_rewrite配置子域名</title>
		<link>http://www.ideawu.net/blog/archives/618.html</link>
		<comments>http://www.ideawu.net/blog/archives/618.html#comments</comments>
		<pubDate>Sun, 04 Sep 2011 14:26:02 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Apache]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/2011/09/apache%e7%94%a8mod_rewrite%e9%85%8d%e7%bd%ae%e5%ad%90%e5%9f%9f%e5%90%8d.html</guid>
		<description><![CDATA[<p>虽然用vhost可以支持子域名, 但不方便.</p>
<pre>
RewriteCond $1 !^bbs/
RewriteCond %{HTTP_HOST} bbs.example.com
RewriteRule (.*) /bbs/$1 [L]
</pre>
<p>RewriteCond $1 !^bbs/ 避免内部无限redirect.</p>
<p><span id="more-618"></span><del datetime="2011-09-04T16:16:59+00:00">或者支持无限子域名:</del></p>
<pre>
RewriteCond %{ENV:Rewrite-Done} !^Yes$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /%1/$1 [E=Rewrite-Done:Yes,L]
</pre>
<p>注: Apache mod_rewrite 有一个严重的BUG, 无法获取自己设置的环境变量. 所以上面的代码有问题! 所以还是半自动:</p>
<pre>
RewriteCond $1 !^bbs/
RewriteCond $1 !^OTHER_SUBDOMAIN/
......
# BUG: RewriteCond %{ENV:Rewrite-Done} !^Yes$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /%1/$1 [E=Rewrite-Done:Yes,L]
</pre>
<p><strong>补充</strong>: <a href="http://xiaobin.net/201109/apache-mapping-subdomain-to-subdir-with-mod_vhost_alias/">xiaobin</a> 提到可以使用 vhost_alias 模块. 我在他的基础之上做了改进, 结合 mod_rewrite, 可以更好的兼容www子域:</p>
<pre>
&lt;VirtualHost *:80&gt;
	ServerAlias ideawu.net *.ideawu.net
	VirtualDocumentRoot /home/work/htdocs/ideawu.net/%1

	RewriteEngine On
	RewriteCond %{HTTP_HOST} ^ideawu.net$
	RewriteRule /(.*) http://www.ideawu.net/$1 [R=301,L]
&lt;/VirtualHost&gt;
</pre>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/447.html' rel='bookmark' title='Permanent Link: Linux下编译安装Apache/Lighttpd+PHP+MySQL'>Linux下编译安装Apache/Lighttpd+PHP+MySQL</a></li>
<li><a href='http://www.ideawu.net/blog/archives/494.html' rel='bookmark' title='Permanent Link: 链接包含&#8221;%2F&#8221;导致mod_rewrite失效'>链接包含&#8221;%2F&#8221;导致mod_rewrite失效</a></li>
<li><a href='http://www.ideawu.net/blog/archives/556.html' rel='bookmark' title='Permanent Link: lighttpd配置HTTPS(SSL)'>lighttpd配置HTTPS(SSL)</a></li>
<li><a href='http://www.ideawu.net/blog/archives/169.html' rel='bookmark' title='Permanent Link: Tomcat网站server.xml设置'>Tomcat网站server.xml设置</a></li>
<li><a href='http://www.ideawu.net/blog/archives/26.html' rel='bookmark' title='Permanent Link: Apache2的httpd.conf翻译'>Apache2的httpd.conf翻译</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/618.html" title="Apache用mod_rewrite配置子域名">Apache用mod_rewrite配置子域名</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>虽然用vhost可以支持子域名, 但不方便.</p>
<pre>
RewriteCond $1 !^bbs/
RewriteCond %{HTTP_HOST} bbs.example.com
RewriteRule (.*) /bbs/$1 [L]
</pre>
<p>RewriteCond $1 !^bbs/ 避免内部无限redirect.</p>
<p><span id="more-618"></span><del datetime="2011-09-04T16:16:59+00:00">或者支持无限子域名:</del></p>
<pre>
RewriteCond %{ENV:Rewrite-Done} !^Yes$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /%1/$1 [E=Rewrite-Done:Yes,L]
</pre>
<p>注: Apache mod_rewrite 有一个严重的BUG, 无法获取自己设置的环境变量. 所以上面的代码有问题! 所以还是半自动:</p>
<pre>
RewriteCond $1 !^bbs/
RewriteCond $1 !^OTHER_SUBDOMAIN/
......
# BUG: RewriteCond %{ENV:Rewrite-Done} !^Yes$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /%1/$1 [E=Rewrite-Done:Yes,L]
</pre>
<p><strong>补充</strong>: <a href="http://xiaobin.net/201109/apache-mapping-subdomain-to-subdir-with-mod_vhost_alias/">xiaobin</a> 提到可以使用 vhost_alias 模块. 我在他的基础之上做了改进, 结合 mod_rewrite, 可以更好的兼容www子域:</p>
<pre>
&lt;VirtualHost *:80&gt;
	ServerAlias ideawu.net *.ideawu.net
	VirtualDocumentRoot /home/work/htdocs/ideawu.net/%1

	RewriteEngine On
	RewriteCond %{HTTP_HOST} ^ideawu.net$
	RewriteRule /(.*) http://www.ideawu.net/$1 [R=301,L]
&lt;/VirtualHost&gt;
</pre>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/447.html' rel='bookmark' title='Permanent Link: Linux下编译安装Apache/Lighttpd+PHP+MySQL'>Linux下编译安装Apache/Lighttpd+PHP+MySQL</a></li>
<li><a href='http://www.ideawu.net/blog/archives/494.html' rel='bookmark' title='Permanent Link: 链接包含&#8221;%2F&#8221;导致mod_rewrite失效'>链接包含&#8221;%2F&#8221;导致mod_rewrite失效</a></li>
<li><a href='http://www.ideawu.net/blog/archives/556.html' rel='bookmark' title='Permanent Link: lighttpd配置HTTPS(SSL)'>lighttpd配置HTTPS(SSL)</a></li>
<li><a href='http://www.ideawu.net/blog/archives/169.html' rel='bookmark' title='Permanent Link: Tomcat网站server.xml设置'>Tomcat网站server.xml设置</a></li>
<li><a href='http://www.ideawu.net/blog/archives/26.html' rel='bookmark' title='Permanent Link: Apache2的httpd.conf翻译'>Apache2的httpd.conf翻译</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/618.html" title="Apache用mod_rewrite配置子域名">Apache用mod_rewrite配置子域名</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/618.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>强大的纯JS数据图工具-flot</title>
		<link>http://www.ideawu.net/blog/archives/611.html</link>
		<comments>http://www.ideawu.net/blog/archives/611.html#comments</comments>
		<pubDate>Wed, 03 Aug 2011 17:03:09 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[flot]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/?p=611</guid>
		<description><![CDATA[<p>发现一个在网页中绘制数据图, 如曲线图, 柱状图的纯 JavaScript 工具: <a href="http://code.google.com/p/flot/">flot</a>. 极度推荐啊! 有图和代码为证:</p>
<p><img src="http://www.ideawu.net/blog/wp-content/uploads/2011/08/flot.png" alt="" title="flot" width="526" height="253" class="alignnone size-full wp-image-612" /></p>
<p>*代码附后*</p>
<p>以前知道的工具有 <a href="http://teethgrinder.co.uk/open-flash-chart/index.php">Open Flash Chart</a>, 还有 Google 出品的 <a href="http://code.google.com/apis/chart/">Google Chart Tool</a>. 两者使用不同的技术, 同时也是两种截然不同的设计理念. 相比较而言, 我更认同 Google Chart 的设计理念. 原因如下:</p>
<p><span id="more-611"></span>Open Flash Chart 使用 Flash 来做图, 这本来也可以接受. 但是, 图形的样式必须通过一个 URL 返回的数据来指定, 而不是网页端技术.</p>
<p>Google Chart Tool 使用 VML 来做图, 同时实现数据和表现的分离, 是 MVC 的思想. 这样的好处是, 同一份数据, 可以用来显示曲线图, 也可以显示成柱状图等等.</p>
<p>显而易见, Open Flash Chart 的技术和理念太陈旧了, 所以不推荐使用. 但是, Google Chart Tool 也有一个重大缺陷, 就是不能离线使用. 当网络状况不好时(在中国大陆经常遇到), 就没法使用了. Google 太霸道, 也不能用.</p>
<p>flot 也是 Google Chart Tool 类似的理念, 所以使用起来非常方便, 而且 demo 代码简单修改就能运行, 学习曲线非常轻松. 而且完美支持 IE6.</p>
<p>附一个可以运行的 flot 例子代码, 将下面的代码保存成 a.html, 然后到 flot 网站下载 JavaScript 代码, 保证路径正确即可用浏览器打开看效果.</p>
<pre>
&lt;html&gt;
&lt;head&gt;
	&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
	&lt;title&gt;flot&lt;/title&gt;
	&lt;!--[if lte IE 8]&gt;
		&lt;script language="javascript" type="text/javascript" src="excanvas.min.js"&gt;&lt;/script&gt;
	&lt;![endif]--&gt;
	&lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt;
	&lt;script type="text/javascript" src="jquery.flot.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;script type="text/javascript"&gt;
&lt;!--
var data = [];
data.push({
    "label": "中国",
    "data": [[2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3], [2004, 2.5], [2005, 2.0], [2006, 3.1]]
});

$(function(){
    var options = {
        lines: { show: true },
        points: { show: true },
        xaxis: { tickDecimals: 0, tickSize: 1 }
    };
    var placeholder = $("#placeholder");
    $.plot(placeholder, data, options);
});
//--&gt;
&lt;/script&gt;

&lt;div id="placeholder" style="width:500px;height:240px"&gt;&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>flot 项目首页: <a href="http://code.google.com/p/flot/">http://code.google.com/p/flot/</a></p>
<p><strong>补充:</strong> 再推荐另外一个非常不错的工具 &#8211; <a href="http://www.highcharts.com/">Highcharts</a>, 看起来功能更强大, 但还没调研使用是否简便.</p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/399.html' rel='bookmark' title='Permanent Link: JavaScript+jQuery两栏选择控件'>JavaScript+jQuery两栏选择控件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/434.html' rel='bookmark' title='Permanent Link: 最简单的JavaScript两级联动示例'>最简单的JavaScript两级联动示例</a></li>
<li><a href='http://www.ideawu.net/blog/archives/270.html' rel='bookmark' title='Permanent Link: 使用Python POST任意的HTTP数据以及使用Cookie'>使用Python POST任意的HTTP数据以及使用Cookie</a></li>
<li><a href='http://www.ideawu.net/blog/archives/412.html' rel='bookmark' title='Permanent Link: JavaScript分页控件'>JavaScript分页控件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/542.html' rel='bookmark' title='Permanent Link: 以浏览器为核心的客户端软件的安全问题'>以浏览器为核心的客户端软件的安全问题</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/611.html" title="强大的纯JS数据图工具-flot">强大的纯JS数据图工具-flot</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>发现一个在网页中绘制数据图, 如曲线图, 柱状图的纯 JavaScript 工具: <a href="http://code.google.com/p/flot/">flot</a>. 极度推荐啊! 有图和代码为证:</p>
<p><img src="http://www.ideawu.net/blog/wp-content/uploads/2011/08/flot.png" alt="" title="flot" width="526" height="253" class="alignnone size-full wp-image-612" /></p>
<p>*代码附后*</p>
<p>以前知道的工具有 <a href="http://teethgrinder.co.uk/open-flash-chart/index.php">Open Flash Chart</a>, 还有 Google 出品的 <a href="http://code.google.com/apis/chart/">Google Chart Tool</a>. 两者使用不同的技术, 同时也是两种截然不同的设计理念. 相比较而言, 我更认同 Google Chart 的设计理念. 原因如下:</p>
<p><span id="more-611"></span>Open Flash Chart 使用 Flash 来做图, 这本来也可以接受. 但是, 图形的样式必须通过一个 URL 返回的数据来指定, 而不是网页端技术.</p>
<p>Google Chart Tool 使用 VML 来做图, 同时实现数据和表现的分离, 是 MVC 的思想. 这样的好处是, 同一份数据, 可以用来显示曲线图, 也可以显示成柱状图等等.</p>
<p>显而易见, Open Flash Chart 的技术和理念太陈旧了, 所以不推荐使用. 但是, Google Chart Tool 也有一个重大缺陷, 就是不能离线使用. 当网络状况不好时(在中国大陆经常遇到), 就没法使用了. Google 太霸道, 也不能用.</p>
<p>flot 也是 Google Chart Tool 类似的理念, 所以使用起来非常方便, 而且 demo 代码简单修改就能运行, 学习曲线非常轻松. 而且完美支持 IE6.</p>
<p>附一个可以运行的 flot 例子代码, 将下面的代码保存成 a.html, 然后到 flot 网站下载 JavaScript 代码, 保证路径正确即可用浏览器打开看效果.</p>
<pre>
&lt;html&gt;
&lt;head&gt;
	&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
	&lt;title&gt;flot&lt;/title&gt;
	&lt;!--[if lte IE 8]&gt;
		&lt;script language="javascript" type="text/javascript" src="excanvas.min.js"&gt;&lt;/script&gt;
	&lt;![endif]--&gt;
	&lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt;
	&lt;script type="text/javascript" src="jquery.flot.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;script type="text/javascript"&gt;
&lt;!--
var data = [];
data.push({
    "label": "中国",
    "data": [[2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3], [2004, 2.5], [2005, 2.0], [2006, 3.1]]
});

$(function(){
    var options = {
        lines: { show: true },
        points: { show: true },
        xaxis: { tickDecimals: 0, tickSize: 1 }
    };
    var placeholder = $("#placeholder");
    $.plot(placeholder, data, options);
});
//--&gt;
&lt;/script&gt;

&lt;div id="placeholder" style="width:500px;height:240px"&gt;&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>flot 项目首页: <a href="http://code.google.com/p/flot/">http://code.google.com/p/flot/</a></p>
<p><strong>补充:</strong> 再推荐另外一个非常不错的工具 &#8211; <a href="http://www.highcharts.com/">Highcharts</a>, 看起来功能更强大, 但还没调研使用是否简便.</p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/399.html' rel='bookmark' title='Permanent Link: JavaScript+jQuery两栏选择控件'>JavaScript+jQuery两栏选择控件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/434.html' rel='bookmark' title='Permanent Link: 最简单的JavaScript两级联动示例'>最简单的JavaScript两级联动示例</a></li>
<li><a href='http://www.ideawu.net/blog/archives/270.html' rel='bookmark' title='Permanent Link: 使用Python POST任意的HTTP数据以及使用Cookie'>使用Python POST任意的HTTP数据以及使用Cookie</a></li>
<li><a href='http://www.ideawu.net/blog/archives/412.html' rel='bookmark' title='Permanent Link: JavaScript分页控件'>JavaScript分页控件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/542.html' rel='bookmark' title='Permanent Link: 以浏览器为核心的客户端软件的安全问题'>以浏览器为核心的客户端软件的安全问题</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/611.html" title="强大的纯JS数据图工具-flot">强大的纯JS数据图工具-flot</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/611.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>获取动态加载的图片大小的正确方法</title>
		<link>http://www.ideawu.net/blog/archives/607.html</link>
		<comments>http://www.ideawu.net/blog/archives/607.html#comments</comments>
		<pubDate>Sun, 03 Jul 2011 14:32:43 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/2011/07/%e8%8e%b7%e5%8f%96%e5%8a%a8%e6%80%81%e5%8a%a0%e8%bd%bd%e7%9a%84%e5%9b%be%e7%89%87%e5%a4%a7%e5%b0%8f%e7%9a%84%e6%ad%a3%e7%a1%ae%e6%96%b9%e6%b3%95.html</guid>
		<description><![CDATA[<p>有一些很容易出错的获取动态加载的图片的尺寸的方法, 之所以出错, 主要原因是:</p>
<ul>
<li>你在代码在图片从网页上下载完毕之前就调用了, 这种情况在本机开发时不太容易发现.</li>
<li>jQuery load()事件处理的BUG, 当图片是从浏览器缓存取得时, 获取的是错误的尺寸.</li>
</ul>
<p>错误的代码是:</p>
<p><span id="more-607"></span></p>
<h3>(错误)在添加了HTML之后立即调用代码获取尺寸</h3>
<pre>
var html = '<img src="URL" />';
$('#my_div').html(html);
var width = $('#my_div img').width(); // may return 0
</pre>
<h3>(错误)用jQuery的load()事件处理</h3>
<pre>
var html = '<img src="URL" />';
var img = $(html);
html.load(function(){
    // return 0 if image is loaded from browser cache
    var width = img.width();
});
$('#my_div').html(img);
</pre>
<p>下面这种才是真正正确的方法, 使用JavaScript的Image类:</p>
<h3>正确的方法</h3>
<pre>
var html = '<img src="URL" />';
$('#my_div').html(html);

var ni = new Image();
ni.onload = function(){
    var width = ni.width;
}
ni.src = img.attr(URL);
</pre>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/477.html' rel='bookmark' title='Permanent Link: jQuery BlockUI 页面遮挡插件'>jQuery BlockUI 页面遮挡插件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/591.html' rel='bookmark' title='Permanent Link: jQuery延时绑定事件(lazy-bind)'>jQuery延时绑定事件(lazy-bind)</a></li>
<li><a href='http://www.ideawu.net/blog/archives/87.html' rel='bookmark' title='Permanent Link: Debian Linux 系统提速'>Debian Linux 系统提速</a></li>
<li><a href='http://www.ideawu.net/blog/archives/611.html' rel='bookmark' title='Permanent Link: 强大的纯JS数据图工具-flot'>强大的纯JS数据图工具-flot</a></li>
<li><a href='http://www.ideawu.net/blog/archives/471.html' rel='bookmark' title='Permanent Link: 最简单的PHP缓存方案 &#8211; Zend_Cache'>最简单的PHP缓存方案 &#8211; Zend_Cache</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/607.html" title="获取动态加载的图片大小的正确方法">获取动态加载的图片大小的正确方法</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>有一些很容易出错的获取动态加载的图片的尺寸的方法, 之所以出错, 主要原因是:</p>
<ul>
<li>你在代码在图片从网页上下载完毕之前就调用了, 这种情况在本机开发时不太容易发现.</li>
<li>jQuery load()事件处理的BUG, 当图片是从浏览器缓存取得时, 获取的是错误的尺寸.</li>
</ul>
<p>错误的代码是:</p>
<p><span id="more-607"></span></p>
<h3>(错误)在添加了HTML之后立即调用代码获取尺寸</h3>
<pre>
var html = '<img src="URL" />';
$('#my_div').html(html);
var width = $('#my_div img').width(); // may return 0
</pre>
<h3>(错误)用jQuery的load()事件处理</h3>
<pre>
var html = '<img src="URL" />';
var img = $(html);
html.load(function(){
    // return 0 if image is loaded from browser cache
    var width = img.width();
});
$('#my_div').html(img);
</pre>
<p>下面这种才是真正正确的方法, 使用JavaScript的Image类:</p>
<h3>正确的方法</h3>
<pre>
var html = '<img src="URL" />';
$('#my_div').html(html);

var ni = new Image();
ni.onload = function(){
    var width = ni.width;
}
ni.src = img.attr(URL);
</pre>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/477.html' rel='bookmark' title='Permanent Link: jQuery BlockUI 页面遮挡插件'>jQuery BlockUI 页面遮挡插件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/591.html' rel='bookmark' title='Permanent Link: jQuery延时绑定事件(lazy-bind)'>jQuery延时绑定事件(lazy-bind)</a></li>
<li><a href='http://www.ideawu.net/blog/archives/87.html' rel='bookmark' title='Permanent Link: Debian Linux 系统提速'>Debian Linux 系统提速</a></li>
<li><a href='http://www.ideawu.net/blog/archives/611.html' rel='bookmark' title='Permanent Link: 强大的纯JS数据图工具-flot'>强大的纯JS数据图工具-flot</a></li>
<li><a href='http://www.ideawu.net/blog/archives/471.html' rel='bookmark' title='Permanent Link: 最简单的PHP缓存方案 &#8211; Zend_Cache'>最简单的PHP缓存方案 &#8211; Zend_Cache</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/607.html" title="获取动态加载的图片大小的正确方法">获取动态加载的图片大小的正确方法</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/607.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>关系数据库应用设计基础</title>
		<link>http://www.ideawu.net/blog/archives/606.html</link>
		<comments>http://www.ideawu.net/blog/archives/606.html#comments</comments>
		<pubDate>Fri, 24 Jun 2011 10:02:57 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[Computer System]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/2011/06/%e5%85%b3%e7%b3%bb%e6%95%b0%e6%8d%ae%e5%ba%93%e5%ba%94%e7%94%a8%e8%ae%be%e8%ae%a1%e5%9f%ba%e7%a1%80.html</guid>
		<description><![CDATA[<p>这是我给部门同事做的技术分享.</p>
<p>当今绝大部分的软件系统都用到了关系数据库, 所以, 作为软件开发工程师, 必须掌握关系数据库应用设计能力.</p>
<div style="width:425px" id="__ss_8410690"> <strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/ideawu/ss-8410690" title="关系数据库应用设计基础">关系数据库应用设计基础</a></strong> <object id="__sse8410690" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=random-110624045756-phpapp02&#038;stripped_title=ss-8410690&#038;userName=ideawu" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse8410690" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=random-110624045756-phpapp02&#038;stripped_title=ss-8410690&#038;userName=ideawu" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="padding:5px 0 12px"> View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/ideawu">ideawu</a> </div>
</p></div>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/589.html' rel='bookmark' title='Permanent Link: 高性能并发Web服务器实现核心内幕'>高性能并发Web服务器实现核心内幕</a></li>
<li><a href='http://www.ideawu.net/blog/archives/482.html' rel='bookmark' title='Permanent Link: 开发爬虫友好的Ajax网站'>开发爬虫友好的Ajax网站</a></li>
<li><a href='http://www.ideawu.net/blog/archives/364.html' rel='bookmark' title='Permanent Link: [不会停止]idea&#8217;s blog 即将停止了&#8230;'>[不会停止]idea&#8217;s blog 即将停止了&#8230;</a></li>
<li><a href='http://www.ideawu.net/blog/archives/355.html' rel='bookmark' title='Permanent Link: 朋友在中关村买华硕笔记本被骗'>朋友在中关村买华硕笔记本被骗</a></li>
<li><a href='http://www.ideawu.net/blog/archives/585.html' rel='bookmark' title='Permanent Link: 史上最强大的PHP Web面试题(会做就能进百度)'>史上最强大的PHP Web面试题(会做就能进百度)</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/606.html" title="关系数据库应用设计基础">关系数据库应用设计基础</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>这是我给部门同事做的技术分享.</p>
<p>当今绝大部分的软件系统都用到了关系数据库, 所以, 作为软件开发工程师, 必须掌握关系数据库应用设计能力.</p>
<div style="width:425px" id="__ss_8410690"> <strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/ideawu/ss-8410690" title="关系数据库应用设计基础">关系数据库应用设计基础</a></strong> <object id="__sse8410690" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=random-110624045756-phpapp02&#038;stripped_title=ss-8410690&#038;userName=ideawu" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse8410690" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=random-110624045756-phpapp02&#038;stripped_title=ss-8410690&#038;userName=ideawu" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="padding:5px 0 12px"> View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/ideawu">ideawu</a> </div>
</p></div>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/589.html' rel='bookmark' title='Permanent Link: 高性能并发Web服务器实现核心内幕'>高性能并发Web服务器实现核心内幕</a></li>
<li><a href='http://www.ideawu.net/blog/archives/482.html' rel='bookmark' title='Permanent Link: 开发爬虫友好的Ajax网站'>开发爬虫友好的Ajax网站</a></li>
<li><a href='http://www.ideawu.net/blog/archives/364.html' rel='bookmark' title='Permanent Link: [不会停止]idea&#8217;s blog 即将停止了&#8230;'>[不会停止]idea&#8217;s blog 即将停止了&#8230;</a></li>
<li><a href='http://www.ideawu.net/blog/archives/355.html' rel='bookmark' title='Permanent Link: 朋友在中关村买华硕笔记本被骗'>朋友在中关村买华硕笔记本被骗</a></li>
<li><a href='http://www.ideawu.net/blog/archives/585.html' rel='bookmark' title='Permanent Link: 史上最强大的PHP Web面试题(会做就能进百度)'>史上最强大的PHP Web面试题(会做就能进百度)</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/606.html" title="关系数据库应用设计基础">关系数据库应用设计基础</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/606.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery延时绑定事件(lazy-bind)</title>
		<link>http://www.ideawu.net/blog/archives/591.html</link>
		<comments>http://www.ideawu.net/blog/archives/591.html#comments</comments>
		<pubDate>Fri, 27 May 2011 08:27:05 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/?p=591</guid>
		<description><![CDATA[<p>有个延时绑定事件的需求, 如等待鼠标停留在某图片上面一段时间之后才展示浮动层, 以避免鼠标滑过屏幕时一片乱闪. 一时找不到合适的插件, 所以自己写了个.</p>
<pre>
// 定义
(function($){
    $.fn.lazybind = function(event, fn, timeout, abort){
        var timer = null;
        $(this).bind(event, function(){
            timer = setTimeout(fn, timeout);
        });
        if(abort == undefined){
            return;
        }
        $(this).bind(abort, function(){
            if(timer != null){
                clearTimeout(timer);
            }
        });
    };
})(jQuery);

// 使用
$('#my_img').lazybind(
    'mouseover',
    function(){
        alert(1);
    },
    240,
    'mouseout');
</pre>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/607.html' rel='bookmark' title='Permanent Link: 获取动态加载的图片大小的正确方法'>获取动态加载的图片大小的正确方法</a></li>
<li><a href='http://www.ideawu.net/blog/archives/477.html' rel='bookmark' title='Permanent Link: jQuery BlockUI 页面遮挡插件'>jQuery BlockUI 页面遮挡插件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/351.html' rel='bookmark' title='Permanent Link: 史上最强大的PHP MySQL操作类'>史上最强大的PHP MySQL操作类</a></li>
<li><a href='http://www.ideawu.net/blog/archives/166.html' rel='bookmark' title='Permanent Link: 使用ServletContextListener在服务器启动和关闭时创建和关闭缓存'>使用ServletContextListener在服务器启动和关闭时创建和关闭缓存</a></li>
<li><a href='http://www.ideawu.net/blog/archives/290.html' rel='bookmark' title='Permanent Link: Ideawu.P2P API 简介'>Ideawu.P2P API 简介</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/591.html" title="jQuery延时绑定事件(lazy-bind)">jQuery延时绑定事件(lazy-bind)</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>有个延时绑定事件的需求, 如等待鼠标停留在某图片上面一段时间之后才展示浮动层, 以避免鼠标滑过屏幕时一片乱闪. 一时找不到合适的插件, 所以自己写了个.</p>
<pre>
// 定义
(function($){
    $.fn.lazybind = function(event, fn, timeout, abort){
        var timer = null;
        $(this).bind(event, function(){
            timer = setTimeout(fn, timeout);
        });
        if(abort == undefined){
            return;
        }
        $(this).bind(abort, function(){
            if(timer != null){
                clearTimeout(timer);
            }
        });
    };
})(jQuery);

// 使用
$('#my_img').lazybind(
    'mouseover',
    function(){
        alert(1);
    },
    240,
    'mouseout');
</pre>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/607.html' rel='bookmark' title='Permanent Link: 获取动态加载的图片大小的正确方法'>获取动态加载的图片大小的正确方法</a></li>
<li><a href='http://www.ideawu.net/blog/archives/477.html' rel='bookmark' title='Permanent Link: jQuery BlockUI 页面遮挡插件'>jQuery BlockUI 页面遮挡插件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/351.html' rel='bookmark' title='Permanent Link: 史上最强大的PHP MySQL操作类'>史上最强大的PHP MySQL操作类</a></li>
<li><a href='http://www.ideawu.net/blog/archives/166.html' rel='bookmark' title='Permanent Link: 使用ServletContextListener在服务器启动和关闭时创建和关闭缓存'>使用ServletContextListener在服务器启动和关闭时创建和关闭缓存</a></li>
<li><a href='http://www.ideawu.net/blog/archives/290.html' rel='bookmark' title='Permanent Link: Ideawu.P2P API 简介'>Ideawu.P2P API 简介</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/591.html" title="jQuery延时绑定事件(lazy-bind)">jQuery延时绑定事件(lazy-bind)</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/591.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>高性能并发Web服务器实现核心内幕</title>
		<link>http://www.ideawu.net/blog/archives/589.html</link>
		<comments>http://www.ideawu.net/blog/archives/589.html#comments</comments>
		<pubDate>Fri, 29 Apr 2011 10:06:54 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[高性能Web架构]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/archives/589.html</guid>
		<description><![CDATA[<p>今天, 和部门的同事一起分享了Web服务器的核心内幕, 把PPT奉上, 大家喜欢可以下载. 本PPT的出发点和许多所谓的XX源码分析非常不同.</p>
<div style="width:425px" id="__ss_7775049"> <strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/ideawu/web-7775049" title="高性能并发Web服务器实现核心内幕">高性能并发Web服务器实现核心内幕</a></strong> <object id="__sse7775049" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=web-110429050116-phpapp02&#038;rel=0&#038;stripped_title=web-7775049&#038;userName=ideawu" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse7775049" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=web-110429050116-phpapp02&#038;rel=0&#038;stripped_title=web-7775049&#038;userName=ideawu" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="padding:5px 0 12px"> View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/ideawu">ideawu</a> </div>
</p></div>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/606.html' rel='bookmark' title='Permanent Link: 关系数据库应用设计基础'>关系数据库应用设计基础</a></li>
<li><a href='http://www.ideawu.net/blog/archives/482.html' rel='bookmark' title='Permanent Link: 开发爬虫友好的Ajax网站'>开发爬虫友好的Ajax网站</a></li>
<li><a href='http://www.ideawu.net/blog/archives/364.html' rel='bookmark' title='Permanent Link: [不会停止]idea&#8217;s blog 即将停止了&#8230;'>[不会停止]idea&#8217;s blog 即将停止了&#8230;</a></li>
<li><a href='http://www.ideawu.net/blog/archives/355.html' rel='bookmark' title='Permanent Link: 朋友在中关村买华硕笔记本被骗'>朋友在中关村买华硕笔记本被骗</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/589.html" title="高性能并发Web服务器实现核心内幕">高性能并发Web服务器实现核心内幕</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>今天, 和部门的同事一起分享了Web服务器的核心内幕, 把PPT奉上, 大家喜欢可以下载. 本PPT的出发点和许多所谓的XX源码分析非常不同.</p>
<div style="width:425px" id="__ss_7775049"> <strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/ideawu/web-7775049" title="高性能并发Web服务器实现核心内幕">高性能并发Web服务器实现核心内幕</a></strong> <object id="__sse7775049" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=web-110429050116-phpapp02&#038;rel=0&#038;stripped_title=web-7775049&#038;userName=ideawu" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse7775049" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=web-110429050116-phpapp02&#038;rel=0&#038;stripped_title=web-7775049&#038;userName=ideawu" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="padding:5px 0 12px"> View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/ideawu">ideawu</a> </div>
</p></div>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/606.html' rel='bookmark' title='Permanent Link: 关系数据库应用设计基础'>关系数据库应用设计基础</a></li>
<li><a href='http://www.ideawu.net/blog/archives/482.html' rel='bookmark' title='Permanent Link: 开发爬虫友好的Ajax网站'>开发爬虫友好的Ajax网站</a></li>
<li><a href='http://www.ideawu.net/blog/archives/364.html' rel='bookmark' title='Permanent Link: [不会停止]idea&#8217;s blog 即将停止了&#8230;'>[不会停止]idea&#8217;s blog 即将停止了&#8230;</a></li>
<li><a href='http://www.ideawu.net/blog/archives/355.html' rel='bookmark' title='Permanent Link: 朋友在中关村买华硕笔记本被骗'>朋友在中关村买华硕笔记本被骗</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/589.html" title="高性能并发Web服务器实现核心内幕">高性能并发Web服务器实现核心内幕</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/589.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>史上最强大的PHP Web面试题(会做就能进百度)</title>
		<link>http://www.ideawu.net/blog/archives/585.html</link>
		<comments>http://www.ideawu.net/blog/archives/585.html#comments</comments>
		<pubDate>Thu, 31 Mar 2011 08:57:24 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[Computer System]]></category>
		<category><![CDATA[IT技术和评论]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[程序员]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/?p=585</guid>
		<description><![CDATA[<p><img src="http://www.ideawu.net/blog/wp-content/uploads/2011/03/cat_tree.jpg" alt="" title="cat_tree" width="246" height="364" class="alignright size-full wp-image-586" /></p>
<blockquote><p>
<strong>注:</strong> 只要你会做了这道题目, 你的能力已经可以进入百度了! 如果别的部门不要你, 请你给我发邮件, 我一定尽我所能强烈推荐你! 如果你不想加入百度, 而别的公司又不要你, 只能说明那家公司瞎眼了.
</p></blockquote>
<p><strong>题目:</strong> 见图片, 该图是某网页的一个区域的截图, 用于显示商品或者其它信息的分类. 该分类的每一项可以折叠和收起(展开和收缩, 如果有子分类的话). 分类的级数不固定. 现有一个PHP变量:</p>
<pre>
$cats = array(
    array(
        'id' => 1,
        'name' => '学术和教育',
        'children' => array(
            array(
                'id' => 2,
                'name' => '自然科学',
                'children' => null,
            ),
            // ...
        ),
    ),
    // ...
);
</pre>
<p>请写一段PHP代码, 将该数组所包含的分类数据生成一段能实现如图片所示功能的HTML/JavaScript代码, 可不考虑CSS样式.</p>
<p>&#8212;&#8212;&#8212;-</p>
<p><strong>注解:</strong> 这道题目考察的范围非常广, 包括<a href="http://www.ideawu.net/blog/category/web/php">PHP</a>, HTML, <a href="http://www.ideawu.net/blog/tag/javascript">JavaScript</a>, CSS, <strong>递归</strong>, 只有真正掌握了如上几种全部技能, 才能实现完整的功能, 否则必须依赖分工. 应聘者所能实现的程度越大, 得分就越高.</p>
<p>如果应聘者的应聘职位不包括HTML/JS/CSS, 那么题目可改为: 把上面的PHP数据用缩进换行文本的形式保存到文件, 并读取文件生成一个同样的PHP数组.(自定义格式的序列化和反序列化)</p>
<p><strong>看到这篇日志的读者, 如果已经做了出来, 并且个人想加入百度, 请在评论中回复URL并说明你的意愿, 我会主动联系你. 或者你可以把程序打包发给我.</strong></p>
<p><span id="more-585"></span><br />
&#8212;&#8212;&#8212;-</p>
<ul style="font-size:15px;background:#dfd;">
<li><a href="http://www.ideawu.net/blog/archives/536.html"><strong>应届生如何成功应聘百度</strong></a></li>
<li><a href="http://www.ideawu.net/blog/archives/481.html"><strong>PHP和百度招聘</strong></a></li>
</ul>
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/351.html' rel='bookmark' title='Permanent Link: 史上最强大的PHP MySQL操作类'>史上最强大的PHP MySQL操作类</a></li>
<li><a href='http://www.ideawu.net/blog/archives/434.html' rel='bookmark' title='Permanent Link: 最简单的JavaScript两级联动示例'>最简单的JavaScript两级联动示例</a></li>
<li><a href='http://www.ideawu.net/blog/archives/399.html' rel='bookmark' title='Permanent Link: JavaScript+jQuery两栏选择控件'>JavaScript+jQuery两栏选择控件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/93.html' rel='bookmark' title='Permanent Link: 国内外一些计算机学习资源的链接'>国内外一些计算机学习资源的链接</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/585.html" title="史上最强大的PHP Web面试题(会做就能进百度)">史上最强大的PHP Web面试题(会做就能进百度)</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.ideawu.net/blog/wp-content/uploads/2011/03/cat_tree.jpg" alt="" title="cat_tree" width="246" height="364" class="alignright size-full wp-image-586" /></p>
<blockquote><p>
<strong>注:</strong> 只要你会做了这道题目, 你的能力已经可以进入百度了! 如果别的部门不要你, 请你给我发邮件, 我一定尽我所能强烈推荐你! 如果你不想加入百度, 而别的公司又不要你, 只能说明那家公司瞎眼了.
</p></blockquote>
<p><strong>题目:</strong> 见图片, 该图是某网页的一个区域的截图, 用于显示商品或者其它信息的分类. 该分类的每一项可以折叠和收起(展开和收缩, 如果有子分类的话). 分类的级数不固定. 现有一个PHP变量:</p>
<pre>
$cats = array(
    array(
        'id' => 1,
        'name' => '学术和教育',
        'children' => array(
            array(
                'id' => 2,
                'name' => '自然科学',
                'children' => null,
            ),
            // ...
        ),
    ),
    // ...
);
</pre>
<p>请写一段PHP代码, 将该数组所包含的分类数据生成一段能实现如图片所示功能的HTML/JavaScript代码, 可不考虑CSS样式.</p>
<p>&#8212;&#8212;&#8212;-</p>
<p><strong>注解:</strong> 这道题目考察的范围非常广, 包括<a href="http://www.ideawu.net/blog/category/web/php">PHP</a>, HTML, <a href="http://www.ideawu.net/blog/tag/javascript">JavaScript</a>, CSS, <strong>递归</strong>, 只有真正掌握了如上几种全部技能, 才能实现完整的功能, 否则必须依赖分工. 应聘者所能实现的程度越大, 得分就越高.</p>
<p>如果应聘者的应聘职位不包括HTML/JS/CSS, 那么题目可改为: 把上面的PHP数据用缩进换行文本的形式保存到文件, 并读取文件生成一个同样的PHP数组.(自定义格式的序列化和反序列化)</p>
<p><strong>看到这篇日志的读者, 如果已经做了出来, 并且个人想加入百度, 请在评论中回复URL并说明你的意愿, 我会主动联系你. 或者你可以把程序打包发给我.</strong></p>
<p><span id="more-585"></span><br />
&#8212;&#8212;&#8212;-</p>
<ul style="font-size:15px;background:#dfd;">
<li><a href="http://www.ideawu.net/blog/archives/536.html"><strong>应届生如何成功应聘百度</strong></a></li>
<li><a href="http://www.ideawu.net/blog/archives/481.html"><strong>PHP和百度招聘</strong></a></li>
</ul>
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/351.html' rel='bookmark' title='Permanent Link: 史上最强大的PHP MySQL操作类'>史上最强大的PHP MySQL操作类</a></li>
<li><a href='http://www.ideawu.net/blog/archives/434.html' rel='bookmark' title='Permanent Link: 最简单的JavaScript两级联动示例'>最简单的JavaScript两级联动示例</a></li>
<li><a href='http://www.ideawu.net/blog/archives/399.html' rel='bookmark' title='Permanent Link: JavaScript+jQuery两栏选择控件'>JavaScript+jQuery两栏选择控件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/93.html' rel='bookmark' title='Permanent Link: 国内外一些计算机学习资源的链接'>国内外一些计算机学习资源的链接</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/585.html" title="史上最强大的PHP Web面试题(会做就能进百度)">史上最强大的PHP Web面试题(会做就能进百度)</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/585.html/feed</wfw:commentRss>
		<slash:comments>52</slash:comments>
		</item>
		<item>
		<title>PHP的continue 2</title>
		<link>http://www.ideawu.net/blog/archives/582.html</link>
		<comments>http://www.ideawu.net/blog/archives/582.html#comments</comments>
		<pubDate>Tue, 01 Mar 2011 05:52:12 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/archives/582.html</guid>
		<description><![CDATA[<p>PHP是一种类C语言语法的脚本语言, 但它有一些和C语言甚至是常见编程语言不一致的地方, 也就是PHP不符合常理的地方. 比如continue指令就是一个鲜活的例子.</p>
<p>简单地把continue用在for循环中, 那么, PHP的continue和C语言的continue一样, 都是在直接跳到下一个循环, 忽略后面的代码的执行. 不过, 如果循环中包含了一个switch语句, 并且continue是放在switch里的, 那么意思就大不相同了!</p>
<p>请看下面的例子:<br />
<span id="more-582"></span></p>
<pre>
[work@ideawu.net ~]$ cat a.php
&lt;?php
for($i=0; $i&lt;6; $i++){
    switch($i){
    case 3:
        continue;
    default:
        break;
    }
    echo $i . "\n";
}

[work@ideawu.net ~]$ php a.php
0
1
<b>2</b>
<b>3</b>
<b>4</b>
5
[work@ideawu.net ~]$ cat t.c
#include &lt;stdio.h&gt;

int main(int argc, char **argv){
    int i;
    for(i=0; i&lt;6; i++){
        switch(i){
            case 3:
                continue;
            default:
                break;
        }
        printf("%d\n", i);
    }
    return 0;
}

[work@ideawu.net ~]$ ./a.out
0
1
<b>2</b>
<b>4</b>
5
</pre>
<p>注意到了吗? PHP的打印结果里出现了数字&#8221;3&#8243;! 也就是说, continue并没有作用到for语句, 这显然和C语言以及其它的语言不一样. 再看PHP的手册, 对这种情况做了解释:</p>
<blockquote><p>
Note: Note that in PHP the <b>switch</b> statement is considered a looping structure for the purposes of continue. </p>
<p>Note: Note that unlike some other languages, the continue statement applies to switch and acts similar to break. If you have a switch inside a loop and wish to continue to the next iteration of the outer loop, use <b>continue 2</b>.
</p></blockquote>
<p>原来, 如果要达到C语言那样的效果, 必须使用&#8221;continue 2(继续二)&#8221;, PHP也<b>太二了</b>吧! 这个陷阱如果不留意, 很容易就陷进去了. 顺便说一句, 没有goto有时极大降低效率.</p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/329.html' rel='bookmark' title='Permanent Link: 一种有趣的编程模型'>一种有趣的编程模型</a></li>
<li><a href='http://www.ideawu.net/blog/archives/470.html' rel='bookmark' title='Permanent Link: Wordpress category widget  using new 2.8 API'>Wordpress category widget  using new 2.8 API</a></li>
<li><a href='http://www.ideawu.net/blog/archives/447.html' rel='bookmark' title='Permanent Link: Linux下编译安装Apache/Lighttpd+PHP+MySQL'>Linux下编译安装Apache/Lighttpd+PHP+MySQL</a></li>
<li><a href='http://www.ideawu.net/blog/archives/505.html' rel='bookmark' title='Permanent Link: PHP解析HTML和loadHTML乱码'>PHP解析HTML和loadHTML乱码</a></li>
<li><a href='http://www.ideawu.net/blog/archives/313.html' rel='bookmark' title='Permanent Link: C# 版的 SimpleXML'>C# 版的 SimpleXML</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/582.html" title="PHP的continue 2">PHP的continue 2</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>PHP是一种类C语言语法的脚本语言, 但它有一些和C语言甚至是常见编程语言不一致的地方, 也就是PHP不符合常理的地方. 比如continue指令就是一个鲜活的例子.</p>
<p>简单地把continue用在for循环中, 那么, PHP的continue和C语言的continue一样, 都是在直接跳到下一个循环, 忽略后面的代码的执行. 不过, 如果循环中包含了一个switch语句, 并且continue是放在switch里的, 那么意思就大不相同了!</p>
<p>请看下面的例子:<br />
<span id="more-582"></span></p>
<pre>
[work@ideawu.net ~]$ cat a.php
&lt;?php
for($i=0; $i&lt;6; $i++){
    switch($i){
    case 3:
        continue;
    default:
        break;
    }
    echo $i . "\n";
}

[work@ideawu.net ~]$ php a.php
0
1
<b>2</b>
<b>3</b>
<b>4</b>
5
[work@ideawu.net ~]$ cat t.c
#include &lt;stdio.h&gt;

int main(int argc, char **argv){
    int i;
    for(i=0; i&lt;6; i++){
        switch(i){
            case 3:
                continue;
            default:
                break;
        }
        printf("%d\n", i);
    }
    return 0;
}

[work@ideawu.net ~]$ ./a.out
0
1
<b>2</b>
<b>4</b>
5
</pre>
<p>注意到了吗? PHP的打印结果里出现了数字&#8221;3&#8243;! 也就是说, continue并没有作用到for语句, 这显然和C语言以及其它的语言不一样. 再看PHP的手册, 对这种情况做了解释:</p>
<blockquote><p>
Note: Note that in PHP the <b>switch</b> statement is considered a looping structure for the purposes of continue. </p>
<p>Note: Note that unlike some other languages, the continue statement applies to switch and acts similar to break. If you have a switch inside a loop and wish to continue to the next iteration of the outer loop, use <b>continue 2</b>.
</p></blockquote>
<p>原来, 如果要达到C语言那样的效果, 必须使用&#8221;continue 2(继续二)&#8221;, PHP也<b>太二了</b>吧! 这个陷阱如果不留意, 很容易就陷进去了. 顺便说一句, 没有goto有时极大降低效率.</p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/329.html' rel='bookmark' title='Permanent Link: 一种有趣的编程模型'>一种有趣的编程模型</a></li>
<li><a href='http://www.ideawu.net/blog/archives/470.html' rel='bookmark' title='Permanent Link: Wordpress category widget  using new 2.8 API'>Wordpress category widget  using new 2.8 API</a></li>
<li><a href='http://www.ideawu.net/blog/archives/447.html' rel='bookmark' title='Permanent Link: Linux下编译安装Apache/Lighttpd+PHP+MySQL'>Linux下编译安装Apache/Lighttpd+PHP+MySQL</a></li>
<li><a href='http://www.ideawu.net/blog/archives/505.html' rel='bookmark' title='Permanent Link: PHP解析HTML和loadHTML乱码'>PHP解析HTML和loadHTML乱码</a></li>
<li><a href='http://www.ideawu.net/blog/archives/313.html' rel='bookmark' title='Permanent Link: C# 版的 SimpleXML'>C# 版的 SimpleXML</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/582.html" title="PHP的continue 2">PHP的continue 2</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/582.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP查询MySQL大量数据的内存占用分析</title>
		<link>http://www.ideawu.net/blog/archives/581.html</link>
		<comments>http://www.ideawu.net/blog/archives/581.html#comments</comments>
		<pubDate>Thu, 24 Feb 2011 02:34:12 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/archives/581.html</guid>
		<description><![CDATA[<p>这篇文章主要是从原理, 手册和源码分析在PHP中查询MySQL返回大量结果时, 内存占用的问题, 同时对使用MySQL C API也有涉及.</p>
<p>昨天, 有同事在PHP讨论群里提到, 他做的一个项目由于MySQL查询返回的结果太多(达10万条), 从而导致PHP内存不够用. 所以, 他问, 在执行下面的代码遍历返回的MySQL结果之前, 数据是否已经在内存中了? -</p>
<pre>
while ($row = mysql_fetch_assoc($result)) {
    // ...
}
</pre>
<p><span id="more-581"></span>当然, 这种问题有许多优化的方法. 不过, 就这个问题来讲, 我首先想到, MySQL是经典的C/S(Client/Server, 客户端/服务器)模型, 在遍历结果集之前, 底层的实现可能已经把所有的数据通过网络(假设使用TCP/IP)读到了Client的缓冲区, 也有另一种可能, 就是数据还在Server端的发送缓冲区里, 并没有传给Client.</p>
<p>在查看PHP和MySQL的源码之前, 我注意到PHP手册里有两个功能相近的函数:</p>
<pre>
mysql_query()
mysql_unbuffered_query()
</pre>
<p>两个函数的字面意思和说明证实了我的想法, 前一个函数执行时, 会把所有的结果集从Server端读到Client端的缓冲区中, 而后一个则没有, 这就是&#8221;unbuffered(未缓冲)&#8221;的意思.</p>
<p>那就是说, 如果用mysql_unbuffered_query()执行了一条返回大量结果集的SQL语句, 在遍历结果之前, PHP的内存是没有被结果集占用的. 而用mysql_query()来执行同样的语句的话, 函数返回时, PHP的内存占用便会急剧增加, 立即耗光内存.</p>
<p>如果阅读PHP的相关代码, 可以看到这两个函数的实现上的异同:</p>
<pre>
/* {{{ proto resource mysql_query(string query [, int link_identifier])
   Sends an SQL query to MySQL */
PHP_FUNCTION(mysql_query)
{
    php_mysql_do_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, <strong>MYSQL_STORE_RESULT</strong>);
}
/* }}} */

/* {{{ proto resource mysql_unbuffered_query(string query [, int link_identifier])
   Sends an SQL query to MySQL, without fetching and buffering the result rows */
PHP_FUNCTION(mysql_unbuffered_query)
{
    php_mysql_do_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, <strong>MYSQL_USE_RESULT</strong>);
}
/* }}} */
</pre>
<p>两个函数都调用了php_mysql_do_query(), 只差了第2个参数的不同, MYSQL_STORE_RESULT和MYSQL_USE_RESULT. 再看php_mysql_do_query()的实现:</p>
<pre>
if(use_store == MYSQL_USE_RESULT) {
    mysql_result=mysql_use_result(&amp;mysql-&gt;conn);
} else {
    mysql_result=mysql_store_result(&amp;mysql-&gt;conn);
}
</pre>
<p>mysql_use_result()和mysql_store_result()是MySQL的C API函数, 这两个C API函数的区别就是后者把结果集从MySQL Server端全部读取到了Client端, 前者只是读取了结果集的元信息.</p>
<p>回到PHP, 使用mysql_unbuffered_query(), 可以避免内存的立即占用. 如果在遍历的过程不对结果进行&#8221;PHP缓存&#8221;(如放到某数组中), 则整个执行过程虽然操作了十万条或者百万条或者更多的数据, 但PHP占用的内存始终是非常小的.</p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/351.html' rel='bookmark' title='Permanent Link: 史上最强大的PHP MySQL操作类'>史上最强大的PHP MySQL操作类</a></li>
<li><a href='http://www.ideawu.net/blog/archives/248.html' rel='bookmark' title='Permanent Link: PHP LDAP连接微软活动目录进行身份验证'>PHP LDAP连接微软活动目录进行身份验证</a></li>
<li><a href='http://www.ideawu.net/blog/archives/133.html' rel='bookmark' title='Permanent Link: 写一个对搜索引擎友好的文章SEO分页类'>写一个对搜索引擎友好的文章SEO分页类</a></li>
<li><a href='http://www.ideawu.net/blog/archives/517.html' rel='bookmark' title='Permanent Link: HTTP POST using PHP cURL'>HTTP POST using PHP cURL</a></li>
<li><a href='http://www.ideawu.net/blog/archives/122.html' rel='bookmark' title='Permanent Link: 编写JSP/PHP+MySQL留言本'>编写JSP/PHP+MySQL留言本</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/581.html" title="PHP查询MySQL大量数据的内存占用分析">PHP查询MySQL大量数据的内存占用分析</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>这篇文章主要是从原理, 手册和源码分析在PHP中查询MySQL返回大量结果时, 内存占用的问题, 同时对使用MySQL C API也有涉及.</p>
<p>昨天, 有同事在PHP讨论群里提到, 他做的一个项目由于MySQL查询返回的结果太多(达10万条), 从而导致PHP内存不够用. 所以, 他问, 在执行下面的代码遍历返回的MySQL结果之前, 数据是否已经在内存中了? -</p>
<pre>
while ($row = mysql_fetch_assoc($result)) {
    // ...
}
</pre>
<p><span id="more-581"></span>当然, 这种问题有许多优化的方法. 不过, 就这个问题来讲, 我首先想到, MySQL是经典的C/S(Client/Server, 客户端/服务器)模型, 在遍历结果集之前, 底层的实现可能已经把所有的数据通过网络(假设使用TCP/IP)读到了Client的缓冲区, 也有另一种可能, 就是数据还在Server端的发送缓冲区里, 并没有传给Client.</p>
<p>在查看PHP和MySQL的源码之前, 我注意到PHP手册里有两个功能相近的函数:</p>
<pre>
mysql_query()
mysql_unbuffered_query()
</pre>
<p>两个函数的字面意思和说明证实了我的想法, 前一个函数执行时, 会把所有的结果集从Server端读到Client端的缓冲区中, 而后一个则没有, 这就是&#8221;unbuffered(未缓冲)&#8221;的意思.</p>
<p>那就是说, 如果用mysql_unbuffered_query()执行了一条返回大量结果集的SQL语句, 在遍历结果之前, PHP的内存是没有被结果集占用的. 而用mysql_query()来执行同样的语句的话, 函数返回时, PHP的内存占用便会急剧增加, 立即耗光内存.</p>
<p>如果阅读PHP的相关代码, 可以看到这两个函数的实现上的异同:</p>
<pre>
/* {{{ proto resource mysql_query(string query [, int link_identifier])
   Sends an SQL query to MySQL */
PHP_FUNCTION(mysql_query)
{
    php_mysql_do_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, <strong>MYSQL_STORE_RESULT</strong>);
}
/* }}} */

/* {{{ proto resource mysql_unbuffered_query(string query [, int link_identifier])
   Sends an SQL query to MySQL, without fetching and buffering the result rows */
PHP_FUNCTION(mysql_unbuffered_query)
{
    php_mysql_do_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, <strong>MYSQL_USE_RESULT</strong>);
}
/* }}} */
</pre>
<p>两个函数都调用了php_mysql_do_query(), 只差了第2个参数的不同, MYSQL_STORE_RESULT和MYSQL_USE_RESULT. 再看php_mysql_do_query()的实现:</p>
<pre>
if(use_store == MYSQL_USE_RESULT) {
    mysql_result=mysql_use_result(&amp;mysql-&gt;conn);
} else {
    mysql_result=mysql_store_result(&amp;mysql-&gt;conn);
}
</pre>
<p>mysql_use_result()和mysql_store_result()是MySQL的C API函数, 这两个C API函数的区别就是后者把结果集从MySQL Server端全部读取到了Client端, 前者只是读取了结果集的元信息.</p>
<p>回到PHP, 使用mysql_unbuffered_query(), 可以避免内存的立即占用. 如果在遍历的过程不对结果进行&#8221;PHP缓存&#8221;(如放到某数组中), 则整个执行过程虽然操作了十万条或者百万条或者更多的数据, 但PHP占用的内存始终是非常小的.</p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/351.html' rel='bookmark' title='Permanent Link: 史上最强大的PHP MySQL操作类'>史上最强大的PHP MySQL操作类</a></li>
<li><a href='http://www.ideawu.net/blog/archives/248.html' rel='bookmark' title='Permanent Link: PHP LDAP连接微软活动目录进行身份验证'>PHP LDAP连接微软活动目录进行身份验证</a></li>
<li><a href='http://www.ideawu.net/blog/archives/133.html' rel='bookmark' title='Permanent Link: 写一个对搜索引擎友好的文章SEO分页类'>写一个对搜索引擎友好的文章SEO分页类</a></li>
<li><a href='http://www.ideawu.net/blog/archives/517.html' rel='bookmark' title='Permanent Link: HTTP POST using PHP cURL'>HTTP POST using PHP cURL</a></li>
<li><a href='http://www.ideawu.net/blog/archives/122.html' rel='bookmark' title='Permanent Link: 编写JSP/PHP+MySQL留言本'>编写JSP/PHP+MySQL留言本</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/581.html" title="PHP查询MySQL大量数据的内存占用分析">PHP查询MySQL大量数据的内存占用分析</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/581.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>30个使用大背景图的酷站</title>
		<link>http://www.ideawu.net/blog/archives/568.html</link>
		<comments>http://www.ideawu.net/blog/archives/568.html#comments</comments>
		<pubDate>Fri, 12 Nov 2010 01:32:05 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/archives/568.html</guid>
		<description><![CDATA[<p>选一个好的背景图对于设计来说永远不会错. 对于网站设计来说, 也是如此, 因为网站设计很大一部分是图片设计. 好的背景图可以立即吸引访问者的目光, 背景图也能决定网站的风格和色调, 甚至是定位.</p>
<h3><a href="http://www.campaninogolfclub.it/">Campanino Golf Club</a></h3>
<p><a href="http://www.campaninogolfclub.it/"><img src="http://www.1stwebdesigner.com/wp-content/uploads/2010/11/campanino-golf-club.jpg" alt="" /></a></p>
<p><span id="more-568"></span></p>
<h3><a href="http://www.ravenatlorabay.com/">Raven at Lora Bay</a></h3>
<p><a href="http://www.ravenatlorabay.com/"><img src="http://www.1stwebdesigner.com/wp-content/uploads/2010/11/raven-at-lora-bay.jpg" alt="" /></a></p>
<h3><a href="http://oldloft.com/">Old Loft</a></h3>
<p><a href="http://oldloft.com/"><img src="http://www.1stwebdesigner.com/wp-content/uploads/2010/11/old-loft-background-design.jpg" alt="" /></a></p>
<p><a href="http://www.1stwebdesigner.com/inspiration/large-photo-background-website-designs/">查看全部30个酷站的设计 &gt;&gt;</a></p>
<p>图片来自: <a href="http://www.1stwebdesigner.com/inspiration/large-photo-background-website-designs/">1stwebdesigner.com</a></p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/271.html' rel='bookmark' title='Permanent Link: CCTV-10 神经兮兮的'>CCTV-10 神经兮兮的</a></li>
<li><a href='http://www.ideawu.net/blog/archives/215.html' rel='bookmark' title='Permanent Link: 世界上最恐怖的厕所及最危险的路'>世界上最恐怖的厕所及最危险的路</a></li>
<li><a href='http://www.ideawu.net/blog/archives/359.html' rel='bookmark' title='Permanent Link: [转]300+Jquery, CSS, MooTools 和 JS的导航菜单'>[转]300+Jquery, CSS, MooTools 和 JS的导航菜单</a></li>
<li><a href='http://www.ideawu.net/blog/archives/618.html' rel='bookmark' title='Permanent Link: Apache用mod_rewrite配置子域名'>Apache用mod_rewrite配置子域名</a></li>
<li><a href='http://www.ideawu.net/blog/archives/502.html' rel='bookmark' title='Permanent Link: tableview新版本发布'>tableview新版本发布</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/568.html" title="30个使用大背景图的酷站">30个使用大背景图的酷站</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>选一个好的背景图对于设计来说永远不会错. 对于网站设计来说, 也是如此, 因为网站设计很大一部分是图片设计. 好的背景图可以立即吸引访问者的目光, 背景图也能决定网站的风格和色调, 甚至是定位.</p>
<h3><a href="http://www.campaninogolfclub.it/">Campanino Golf Club</a></h3>
<p><a href="http://www.campaninogolfclub.it/"><img src="http://www.1stwebdesigner.com/wp-content/uploads/2010/11/campanino-golf-club.jpg" alt="" /></a></p>
<p><span id="more-568"></span></p>
<h3><a href="http://www.ravenatlorabay.com/">Raven at Lora Bay</a></h3>
<p><a href="http://www.ravenatlorabay.com/"><img src="http://www.1stwebdesigner.com/wp-content/uploads/2010/11/raven-at-lora-bay.jpg" alt="" /></a></p>
<h3><a href="http://oldloft.com/">Old Loft</a></h3>
<p><a href="http://oldloft.com/"><img src="http://www.1stwebdesigner.com/wp-content/uploads/2010/11/old-loft-background-design.jpg" alt="" /></a></p>
<p><a href="http://www.1stwebdesigner.com/inspiration/large-photo-background-website-designs/">查看全部30个酷站的设计 &gt;&gt;</a></p>
<p>图片来自: <a href="http://www.1stwebdesigner.com/inspiration/large-photo-background-website-designs/">1stwebdesigner.com</a></p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/271.html' rel='bookmark' title='Permanent Link: CCTV-10 神经兮兮的'>CCTV-10 神经兮兮的</a></li>
<li><a href='http://www.ideawu.net/blog/archives/215.html' rel='bookmark' title='Permanent Link: 世界上最恐怖的厕所及最危险的路'>世界上最恐怖的厕所及最危险的路</a></li>
<li><a href='http://www.ideawu.net/blog/archives/359.html' rel='bookmark' title='Permanent Link: [转]300+Jquery, CSS, MooTools 和 JS的导航菜单'>[转]300+Jquery, CSS, MooTools 和 JS的导航菜单</a></li>
<li><a href='http://www.ideawu.net/blog/archives/618.html' rel='bookmark' title='Permanent Link: Apache用mod_rewrite配置子域名'>Apache用mod_rewrite配置子域名</a></li>
<li><a href='http://www.ideawu.net/blog/archives/502.html' rel='bookmark' title='Permanent Link: tableview新版本发布'>tableview新版本发布</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/568.html" title="30个使用大背景图的酷站">30个使用大背景图的酷站</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/568.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>lighttpd配置HTTPS(SSL)</title>
		<link>http://www.ideawu.net/blog/archives/556.html</link>
		<comments>http://www.ideawu.net/blog/archives/556.html#comments</comments>
		<pubDate>Fri, 29 Oct 2010 02:45:18 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/archives/556.html</guid>
		<description><![CDATA[<p>1. 创建SSL证书</p>
<p>openssl req -new -x509 \<br />
-keyout ideawu.net.pem -out ideawu.net.pem \<br />
-days 99365 -nodes</p>
<p>把 ideawu.net.pem 拷贝到 /home/work/lighttpd-1.5/conf/</p>
<p>2. lighttpd配置</p>
<pre>
$SERVER["socket"] == ":443" {
    $HTTP["host"] == "ideawu.net" {
        ssl.engine = "enable"
        ssl.pemfile = "/home/work/lighttpd-1.5/conf/ideawu.net.pem"
    }
}
</pre>
<p>3. 访问:</p>
<p>https://localhost:443</p>
<p>4. API和工具使用:</p>
<p>wget &#8211;no-check-certificate https://localhost:443</p>
<p>curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);<br />
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);</p>
<p><b>补充: Apache HTTPS</b><br />
openssl genrsa 1024 > server.key<br />
openssl req -new -key server.key &gt; server.csr<br />
openssl req -x509 -days 99365 -key server.key -in server.csr &gt; server.crt</p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/169.html' rel='bookmark' title='Permanent Link: Tomcat网站server.xml设置'>Tomcat网站server.xml设置</a></li>
<li><a href='http://www.ideawu.net/blog/archives/447.html' rel='bookmark' title='Permanent Link: Linux下编译安装Apache/Lighttpd+PHP+MySQL'>Linux下编译安装Apache/Lighttpd+PHP+MySQL</a></li>
<li><a href='http://www.ideawu.net/blog/archives/517.html' rel='bookmark' title='Permanent Link: HTTP POST using PHP cURL'>HTTP POST using PHP cURL</a></li>
<li><a href='http://www.ideawu.net/blog/archives/622.html' rel='bookmark' title='Permanent Link: PHP重用curl句柄, CURLOPT_HTTPGET的BUG'>PHP重用curl句柄, CURLOPT_HTTPGET的BUG</a></li>
<li><a href='http://www.ideawu.net/blog/archives/98.html' rel='bookmark' title='Permanent Link: Linux下整合Apache和Tomcat'>Linux下整合Apache和Tomcat</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/556.html" title="lighttpd配置HTTPS(SSL)">lighttpd配置HTTPS(SSL)</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>1. 创建SSL证书</p>
<p>openssl req -new -x509 \<br />
-keyout ideawu.net.pem -out ideawu.net.pem \<br />
-days 99365 -nodes</p>
<p>把 ideawu.net.pem 拷贝到 /home/work/lighttpd-1.5/conf/</p>
<p>2. lighttpd配置</p>
<pre>
$SERVER["socket"] == ":443" {
    $HTTP["host"] == "ideawu.net" {
        ssl.engine = "enable"
        ssl.pemfile = "/home/work/lighttpd-1.5/conf/ideawu.net.pem"
    }
}
</pre>
<p>3. 访问:</p>
<p>https://localhost:443</p>
<p>4. API和工具使用:</p>
<p>wget &#8211;no-check-certificate https://localhost:443</p>
<p>curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);<br />
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);</p>
<p><b>补充: Apache HTTPS</b><br />
openssl genrsa 1024 > server.key<br />
openssl req -new -key server.key &gt; server.csr<br />
openssl req -x509 -days 99365 -key server.key -in server.csr &gt; server.crt</p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/169.html' rel='bookmark' title='Permanent Link: Tomcat网站server.xml设置'>Tomcat网站server.xml设置</a></li>
<li><a href='http://www.ideawu.net/blog/archives/447.html' rel='bookmark' title='Permanent Link: Linux下编译安装Apache/Lighttpd+PHP+MySQL'>Linux下编译安装Apache/Lighttpd+PHP+MySQL</a></li>
<li><a href='http://www.ideawu.net/blog/archives/517.html' rel='bookmark' title='Permanent Link: HTTP POST using PHP cURL'>HTTP POST using PHP cURL</a></li>
<li><a href='http://www.ideawu.net/blog/archives/622.html' rel='bookmark' title='Permanent Link: PHP重用curl句柄, CURLOPT_HTTPGET的BUG'>PHP重用curl句柄, CURLOPT_HTTPGET的BUG</a></li>
<li><a href='http://www.ideawu.net/blog/archives/98.html' rel='bookmark' title='Permanent Link: Linux下整合Apache和Tomcat'>Linux下整合Apache和Tomcat</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/556.html" title="lighttpd配置HTTPS(SSL)">lighttpd配置HTTPS(SSL)</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/556.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>tableview新增单选功能</title>
		<link>http://www.ideawu.net/blog/archives/554.html</link>
		<comments>http://www.ideawu.net/blog/archives/554.html#comments</comments>
		<pubDate>Sat, 23 Oct 2010 08:49:24 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/?p=554</guid>
		<description><![CDATA[<p>根据用户<a href="http://www.ideawu.net/blog/guestbook#comment-1696">反馈</a>的功能需求, tableview 现新增了单选功能, 用于限制只能选择最多一行记录.</p>
<p><strong>tableview </strong>是一组功能丰富, 接口易学易用的 javascript 控件, 包括数据表格控件, 排序控件, 分页控件, 双栏选择控件. <strong>下载及文档:</strong> <a href="http://www.ideawu.net/person/tableview/">http://www.ideawu.net/person/tableview/</a></p>
<p><a href="http://www.ideawu.net/person/tableview/"><img src="http://www.ideawu.net/blog/wp-content/uploads/2010/05/tableview.jpg" alt="" title="tableview" width="427" height="218" class="alignnone size-full wp-image-501" /></a></p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/502.html' rel='bookmark' title='Permanent Link: tableview新版本发布'>tableview新版本发布</a></li>
<li><a href='http://www.ideawu.net/blog/archives/399.html' rel='bookmark' title='Permanent Link: JavaScript+jQuery两栏选择控件'>JavaScript+jQuery两栏选择控件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/451.html' rel='bookmark' title='Permanent Link: JavaScript数据表格和分页控件文档升级'>JavaScript数据表格和分页控件文档升级</a></li>
<li><a href='http://www.ideawu.net/blog/archives/412.html' rel='bookmark' title='Permanent Link: JavaScript分页控件'>JavaScript分页控件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/452.html' rel='bookmark' title='Permanent Link: JavaScript年会抽奖软件'>JavaScript年会抽奖软件</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/554.html" title="tableview新增单选功能">tableview新增单选功能</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>根据用户<a href="http://www.ideawu.net/blog/guestbook#comment-1696">反馈</a>的功能需求, tableview 现新增了单选功能, 用于限制只能选择最多一行记录.</p>
<p><strong>tableview </strong>是一组功能丰富, 接口易学易用的 javascript 控件, 包括数据表格控件, 排序控件, 分页控件, 双栏选择控件. <strong>下载及文档:</strong> <a href="http://www.ideawu.net/person/tableview/">http://www.ideawu.net/person/tableview/</a></p>
<p><a href="http://www.ideawu.net/person/tableview/"><img src="http://www.ideawu.net/blog/wp-content/uploads/2010/05/tableview.jpg" alt="" title="tableview" width="427" height="218" class="alignnone size-full wp-image-501" /></a></p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/502.html' rel='bookmark' title='Permanent Link: tableview新版本发布'>tableview新版本发布</a></li>
<li><a href='http://www.ideawu.net/blog/archives/399.html' rel='bookmark' title='Permanent Link: JavaScript+jQuery两栏选择控件'>JavaScript+jQuery两栏选择控件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/451.html' rel='bookmark' title='Permanent Link: JavaScript数据表格和分页控件文档升级'>JavaScript数据表格和分页控件文档升级</a></li>
<li><a href='http://www.ideawu.net/blog/archives/412.html' rel='bookmark' title='Permanent Link: JavaScript分页控件'>JavaScript分页控件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/452.html' rel='bookmark' title='Permanent Link: JavaScript年会抽奖软件'>JavaScript年会抽奖软件</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/554.html" title="tableview新增单选功能">tableview新增单选功能</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/554.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Wordpress评论转义HTML标签</title>
		<link>http://www.ideawu.net/blog/archives/519.html</link>
		<comments>http://www.ideawu.net/blog/archives/519.html#comments</comments>
		<pubDate>Tue, 22 Jun 2010 05:54:47 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/archives/519.html</guid>
		<description><![CDATA[<p>把这段代码放到 theme 里的 functions.php 文件:</p>
<pre>
function theme_pre_comment_post( $c) {
	$c['comment_content'] = htmlspecialchars($c['comment_content']);
	return $c;
}
add_filter( 'preprocess_comment', 'theme_pre_comment_post', '', 1);
</pre>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/591.html' rel='bookmark' title='Permanent Link: jQuery延时绑定事件(lazy-bind)'>jQuery延时绑定事件(lazy-bind)</a></li>
<li><a href='http://www.ideawu.net/blog/archives/517.html' rel='bookmark' title='Permanent Link: HTTP POST using PHP cURL'>HTTP POST using PHP cURL</a></li>
<li><a href='http://www.ideawu.net/blog/archives/351.html' rel='bookmark' title='Permanent Link: 史上最强大的PHP MySQL操作类'>史上最强大的PHP MySQL操作类</a></li>
<li><a href='http://www.ideawu.net/blog/archives/607.html' rel='bookmark' title='Permanent Link: 获取动态加载的图片大小的正确方法'>获取动态加载的图片大小的正确方法</a></li>
<li><a href='http://www.ideawu.net/blog/archives/202.html' rel='bookmark' title='Permanent Link: 写自己的 http_build_query'>写自己的 http_build_query</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/519.html" title="Wordpress评论转义HTML标签">Wordpress评论转义HTML标签</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>把这段代码放到 theme 里的 functions.php 文件:</p>
<pre>
function theme_pre_comment_post( $c) {
	$c['comment_content'] = htmlspecialchars($c['comment_content']);
	return $c;
}
add_filter( 'preprocess_comment', 'theme_pre_comment_post', '', 1);
</pre>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/591.html' rel='bookmark' title='Permanent Link: jQuery延时绑定事件(lazy-bind)'>jQuery延时绑定事件(lazy-bind)</a></li>
<li><a href='http://www.ideawu.net/blog/archives/517.html' rel='bookmark' title='Permanent Link: HTTP POST using PHP cURL'>HTTP POST using PHP cURL</a></li>
<li><a href='http://www.ideawu.net/blog/archives/351.html' rel='bookmark' title='Permanent Link: 史上最强大的PHP MySQL操作类'>史上最强大的PHP MySQL操作类</a></li>
<li><a href='http://www.ideawu.net/blog/archives/607.html' rel='bookmark' title='Permanent Link: 获取动态加载的图片大小的正确方法'>获取动态加载的图片大小的正确方法</a></li>
<li><a href='http://www.ideawu.net/blog/archives/202.html' rel='bookmark' title='Permanent Link: 写自己的 http_build_query'>写自己的 http_build_query</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/519.html" title="Wordpress评论转义HTML标签">Wordpress评论转义HTML标签</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/519.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTTP POST using PHP cURL</title>
		<link>http://www.ideawu.net/blog/archives/517.html</link>
		<comments>http://www.ideawu.net/blog/archives/517.html#comments</comments>
		<pubDate>Mon, 21 Jun 2010 06:44:40 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[HTTP]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/archives/517.html</guid>
		<description><![CDATA[<pre>
function http_post($url, $data){
	$ch = curl_init($url) ;
	curl_setopt($ch, CURLOPT_POST, 1) ;
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ;
	$result = curl_exec($ch) ;
	curl_close($ch) ;

	return $result;
}

function http_get($url){
	$ch = curl_init($url) ;
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ;
	$result = curl_exec($ch) ;
	curl_close($ch) ;

	return $result;
}
</pre>
<p>获取 HTTP 请求的首部:</p>
<pre>
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$result = curl_exec($ch) ;
echo curl_getinfo($ch, CURLINFO_HEADER_OUT);
</pre>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/622.html' rel='bookmark' title='Permanent Link: PHP重用curl句柄, CURLOPT_HTTPGET的BUG'>PHP重用curl句柄, CURLOPT_HTTPGET的BUG</a></li>
<li><a href='http://www.ideawu.net/blog/archives/351.html' rel='bookmark' title='Permanent Link: 史上最强大的PHP MySQL操作类'>史上最强大的PHP MySQL操作类</a></li>
<li><a href='http://www.ideawu.net/blog/archives/264.html' rel='bookmark' title='Permanent Link: 通过 HTTP POST 发送二进制数据'>通过 HTTP POST 发送二进制数据</a></li>
<li><a href='http://www.ideawu.net/blog/archives/202.html' rel='bookmark' title='Permanent Link: 写自己的 http_build_query'>写自己的 http_build_query</a></li>
<li><a href='http://www.ideawu.net/blog/archives/367.html' rel='bookmark' title='Permanent Link: Wordpress分页代码'>Wordpress分页代码</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/517.html" title="HTTP POST using PHP cURL">HTTP POST using PHP cURL</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<pre>
function http_post($url, $data){
	$ch = curl_init($url) ;
	curl_setopt($ch, CURLOPT_POST, 1) ;
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ;
	$result = curl_exec($ch) ;
	curl_close($ch) ;

	return $result;
}

function http_get($url){
	$ch = curl_init($url) ;
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ;
	$result = curl_exec($ch) ;
	curl_close($ch) ;

	return $result;
}
</pre>
<p>获取 HTTP 请求的首部:</p>
<pre>
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$result = curl_exec($ch) ;
echo curl_getinfo($ch, CURLINFO_HEADER_OUT);
</pre>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/622.html' rel='bookmark' title='Permanent Link: PHP重用curl句柄, CURLOPT_HTTPGET的BUG'>PHP重用curl句柄, CURLOPT_HTTPGET的BUG</a></li>
<li><a href='http://www.ideawu.net/blog/archives/351.html' rel='bookmark' title='Permanent Link: 史上最强大的PHP MySQL操作类'>史上最强大的PHP MySQL操作类</a></li>
<li><a href='http://www.ideawu.net/blog/archives/264.html' rel='bookmark' title='Permanent Link: 通过 HTTP POST 发送二进制数据'>通过 HTTP POST 发送二进制数据</a></li>
<li><a href='http://www.ideawu.net/blog/archives/202.html' rel='bookmark' title='Permanent Link: 写自己的 http_build_query'>写自己的 http_build_query</a></li>
<li><a href='http://www.ideawu.net/blog/archives/367.html' rel='bookmark' title='Permanent Link: Wordpress分页代码'>Wordpress分页代码</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/517.html" title="HTTP POST using PHP cURL">HTTP POST using PHP cURL</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/517.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP解析HTML和loadHTML乱码</title>
		<link>http://www.ideawu.net/blog/archives/505.html</link>
		<comments>http://www.ideawu.net/blog/archives/505.html#comments</comments>
		<pubDate>Wed, 19 May 2010 05:44:57 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/archives/505.html</guid>
		<description><![CDATA[<p>为了对 RSS 输出的博客文章进行重新格式化, 比如去掉过于花哨的样式, 去除 JavaScript 脚本代码, 去除 onclick 等, 所以写了相关的 PHP 代码, 使用 DOM 模块.</p>
<h3>1. 乱码解决</h3>
<p>毫无疑问, 一上来就遇到了乱码问题, 虽然我已经按文档所述, 所有的字符使用 UTF-8 编码:</p>
<pre>
$html = '&lt;p&gt;你好&lt;/p&gt;';
$dom = new DOMDocument();
@$dom-&gt;loadHTML($html);
echo $dom-&gt;documentElement-&gt;nodeValue;
</pre>
<p><span id="more-505"></span>但是, 如果改成:</p>
<pre>
$html = '&lt;p&gt;你好&lt;/p&gt;';
$dom = new DOMDocument();
@$dom-&gt;loadXML($html);
echo $dom-&gt;documentElement-&gt;nodeValue;
</pre>
<p>就没有问题. 后来才发现, 原来 loadHTML 会依赖 HTML 中的声明 meta 标签. 如果没有这样的标签, 就当作 iso-8859-1 字符集, 所以乱码. 要解决, 就给字符串加上这样的一个标签在头部:</p>
<pre>
$meta = '&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;';
@$dom-&gt;loadHTML($meta . $html);
</pre>
<h3>2. 递归</h3>
<p>HTML/XML 是递归结构, 所以一定会递归遍历:</p>
<pre>
function _pretty_html_node($node){
	// 递归终止条件
	// 1. XML_TEXT_NODE
	// 2. XML_ELEMENT_NODE
	// 3. 没有子节点

	foreach($node-&gt;childNodes as $n){
		$child_text .= _pretty_html_node($n);
	}

	// 然后对不同的标签做不同的处理
	switch($tag){
		case 'a':
			$href = $node-&gt;getAttribute('href');
			$text .= "&lt;a href=\"$href\"&gt;$child_text&lt;/a&gt;";
		...
	}

	return $text;
}
</pre>
<h3>3. 转义字符处理</h3>
<p>对于文本节点, 其 nodeValue 要经过 htmlspeciachars() 转义. 因为读取 HTML/XML 时, 会对文本进行反转义, 比如 &amp;gt; 在内存中已经是 &gt;了.</p>
<p>下载源码: <a href='http://www.ideawu.net/blog/wp-content/uploads/2010/05/pretty_html.php_.zip'>pretty_html.php</a></p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/313.html' rel='bookmark' title='Permanent Link: C# 版的 SimpleXML'>C# 版的 SimpleXML</a></li>
<li><a href='http://www.ideawu.net/blog/archives/55.html' rel='bookmark' title='Permanent Link: 自架设Apache服务器过程中的网页乱码问题'>自架设Apache服务器过程中的网页乱码问题</a></li>
<li><a href='http://www.ideawu.net/blog/archives/367.html' rel='bookmark' title='Permanent Link: Wordpress分页代码'>Wordpress分页代码</a></li>
<li><a href='http://www.ideawu.net/blog/archives/518.html' rel='bookmark' title='Permanent Link: if-else对优化代码冗余度的反作用'>if-else对优化代码冗余度的反作用</a></li>
<li><a href='http://www.ideawu.net/blog/archives/611.html' rel='bookmark' title='Permanent Link: 强大的纯JS数据图工具-flot'>强大的纯JS数据图工具-flot</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/505.html" title="PHP解析HTML和loadHTML乱码">PHP解析HTML和loadHTML乱码</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>为了对 RSS 输出的博客文章进行重新格式化, 比如去掉过于花哨的样式, 去除 JavaScript 脚本代码, 去除 onclick 等, 所以写了相关的 PHP 代码, 使用 DOM 模块.</p>
<h3>1. 乱码解决</h3>
<p>毫无疑问, 一上来就遇到了乱码问题, 虽然我已经按文档所述, 所有的字符使用 UTF-8 编码:</p>
<pre>
$html = '&lt;p&gt;你好&lt;/p&gt;';
$dom = new DOMDocument();
@$dom-&gt;loadHTML($html);
echo $dom-&gt;documentElement-&gt;nodeValue;
</pre>
<p><span id="more-505"></span>但是, 如果改成:</p>
<pre>
$html = '&lt;p&gt;你好&lt;/p&gt;';
$dom = new DOMDocument();
@$dom-&gt;loadXML($html);
echo $dom-&gt;documentElement-&gt;nodeValue;
</pre>
<p>就没有问题. 后来才发现, 原来 loadHTML 会依赖 HTML 中的声明 meta 标签. 如果没有这样的标签, 就当作 iso-8859-1 字符集, 所以乱码. 要解决, 就给字符串加上这样的一个标签在头部:</p>
<pre>
$meta = '&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;';
@$dom-&gt;loadHTML($meta . $html);
</pre>
<h3>2. 递归</h3>
<p>HTML/XML 是递归结构, 所以一定会递归遍历:</p>
<pre>
function _pretty_html_node($node){
	// 递归终止条件
	// 1. XML_TEXT_NODE
	// 2. XML_ELEMENT_NODE
	// 3. 没有子节点

	foreach($node-&gt;childNodes as $n){
		$child_text .= _pretty_html_node($n);
	}

	// 然后对不同的标签做不同的处理
	switch($tag){
		case 'a':
			$href = $node-&gt;getAttribute('href');
			$text .= "&lt;a href=\"$href\"&gt;$child_text&lt;/a&gt;";
		...
	}

	return $text;
}
</pre>
<h3>3. 转义字符处理</h3>
<p>对于文本节点, 其 nodeValue 要经过 htmlspeciachars() 转义. 因为读取 HTML/XML 时, 会对文本进行反转义, 比如 &amp;gt; 在内存中已经是 &gt;了.</p>
<p>下载源码: <a href='http://www.ideawu.net/blog/wp-content/uploads/2010/05/pretty_html.php_.zip'>pretty_html.php</a></p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/313.html' rel='bookmark' title='Permanent Link: C# 版的 SimpleXML'>C# 版的 SimpleXML</a></li>
<li><a href='http://www.ideawu.net/blog/archives/55.html' rel='bookmark' title='Permanent Link: 自架设Apache服务器过程中的网页乱码问题'>自架设Apache服务器过程中的网页乱码问题</a></li>
<li><a href='http://www.ideawu.net/blog/archives/367.html' rel='bookmark' title='Permanent Link: Wordpress分页代码'>Wordpress分页代码</a></li>
<li><a href='http://www.ideawu.net/blog/archives/518.html' rel='bookmark' title='Permanent Link: if-else对优化代码冗余度的反作用'>if-else对优化代码冗余度的反作用</a></li>
<li><a href='http://www.ideawu.net/blog/archives/611.html' rel='bookmark' title='Permanent Link: 强大的纯JS数据图工具-flot'>强大的纯JS数据图工具-flot</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/505.html" title="PHP解析HTML和loadHTML乱码">PHP解析HTML和loadHTML乱码</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/505.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>tableview新版本发布</title>
		<link>http://www.ideawu.net/blog/archives/502.html</link>
		<comments>http://www.ideawu.net/blog/archives/502.html#comments</comments>
		<pubDate>Wed, 12 May 2010 14:16:46 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/archives/502.html</guid>
		<description><![CDATA[<p><b>tableview</b> 即原来的 <a href="http://www.ideawu.net/person/two-column-selector/">two-column-selector</a>, 在升级为 1.1 版本时, 它的项目名称变为 tableview. 1.1 版本最大的变化是加入了<a href="http://www.ideawu.net/person/tableview/v1.1/SortView.php">排序功能</a>.</p>
<p>在网页开发中, 常常需要一个表格与列表相结合的控件, 即能像表格(table)一样显示丰富的信息, 又能像列表选择控件(select)一样方便地选择数据. 在桌面应用程序开发中, 许多GUI库都提供了类似的控件, 如.Net的ListView.</p>
<ul>
<li>
		PagerView是一个好看精巧的分页控件. PagerView+TableView为网页开发提供了类似的工具, 但代码更简单, 功能更紧凑.
	</li>
<li>
		SortView是一个方便易用的分页控件.
	</li>
<li>
		TableView是一个数据表格控件, 代码简单, 功能紧凑.
	</li>
<li>
		SelectorView是一个由两个TableView组合而成的控件, 相对于列表选择器, 提供了一个临时的存放已选中项的地方.
	</li>
</ul>
<p><strong>下载及文档:</strong> <a href="http://www.ideawu.net/person/tableview/v1.1/">http://www.ideawu.net/person/tableview/v1.1/</a></p>
<p><a href="http://www.ideawu.net/blog/wp-content/uploads/2010/05/tableview.jpg"><img src="http://www.ideawu.net/blog/wp-content/uploads/2010/05/tableview.jpg" alt="" title="tableview" width="427" height="218" class="alignnone size-full wp-image-501" /></a></p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/554.html' rel='bookmark' title='Permanent Link: tableview新增单选功能'>tableview新增单选功能</a></li>
<li><a href='http://www.ideawu.net/blog/archives/451.html' rel='bookmark' title='Permanent Link: JavaScript数据表格和分页控件文档升级'>JavaScript数据表格和分页控件文档升级</a></li>
<li><a href='http://www.ideawu.net/blog/archives/412.html' rel='bookmark' title='Permanent Link: JavaScript分页控件'>JavaScript分页控件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/399.html' rel='bookmark' title='Permanent Link: JavaScript+jQuery两栏选择控件'>JavaScript+jQuery两栏选择控件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/452.html' rel='bookmark' title='Permanent Link: JavaScript年会抽奖软件'>JavaScript年会抽奖软件</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/502.html" title="tableview新版本发布">tableview新版本发布</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p><b>tableview</b> 即原来的 <a href="http://www.ideawu.net/person/two-column-selector/">two-column-selector</a>, 在升级为 1.1 版本时, 它的项目名称变为 tableview. 1.1 版本最大的变化是加入了<a href="http://www.ideawu.net/person/tableview/v1.1/SortView.php">排序功能</a>.</p>
<p>在网页开发中, 常常需要一个表格与列表相结合的控件, 即能像表格(table)一样显示丰富的信息, 又能像列表选择控件(select)一样方便地选择数据. 在桌面应用程序开发中, 许多GUI库都提供了类似的控件, 如.Net的ListView.</p>
<ul>
<li>
		PagerView是一个好看精巧的分页控件. PagerView+TableView为网页开发提供了类似的工具, 但代码更简单, 功能更紧凑.
	</li>
<li>
		SortView是一个方便易用的分页控件.
	</li>
<li>
		TableView是一个数据表格控件, 代码简单, 功能紧凑.
	</li>
<li>
		SelectorView是一个由两个TableView组合而成的控件, 相对于列表选择器, 提供了一个临时的存放已选中项的地方.
	</li>
</ul>
<p><strong>下载及文档:</strong> <a href="http://www.ideawu.net/person/tableview/v1.1/">http://www.ideawu.net/person/tableview/v1.1/</a></p>
<p><a href="http://www.ideawu.net/blog/wp-content/uploads/2010/05/tableview.jpg"><img src="http://www.ideawu.net/blog/wp-content/uploads/2010/05/tableview.jpg" alt="" title="tableview" width="427" height="218" class="alignnone size-full wp-image-501" /></a></p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/554.html' rel='bookmark' title='Permanent Link: tableview新增单选功能'>tableview新增单选功能</a></li>
<li><a href='http://www.ideawu.net/blog/archives/451.html' rel='bookmark' title='Permanent Link: JavaScript数据表格和分页控件文档升级'>JavaScript数据表格和分页控件文档升级</a></li>
<li><a href='http://www.ideawu.net/blog/archives/412.html' rel='bookmark' title='Permanent Link: JavaScript分页控件'>JavaScript分页控件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/399.html' rel='bookmark' title='Permanent Link: JavaScript+jQuery两栏选择控件'>JavaScript+jQuery两栏选择控件</a></li>
<li><a href='http://www.ideawu.net/blog/archives/452.html' rel='bookmark' title='Permanent Link: JavaScript年会抽奖软件'>JavaScript年会抽奖软件</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/502.html" title="tableview新版本发布">tableview新版本发布</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/502.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>开发搜索引擎 &#8211; PHP中文分词</title>
		<link>http://www.ideawu.net/blog/archives/495.html</link>
		<comments>http://www.ideawu.net/blog/archives/495.html#comments</comments>
		<pubDate>Fri, 07 May 2010 00:30:07 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[ICTCLAS]]></category>
		<category><![CDATA[Lucene]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/archives/495.html</guid>
		<description><![CDATA[<p>对于中文搜索引擎来说, 中文分词是整个系统最基础的部分之一, 因为目前基于单字的中文搜索算法并不是太好. 当然, 本文不是要对中文搜索引擎做研究, 而是分享如果用 PHP 做一个站内搜索引擎. 本文是这个系统中的一篇.</p>
<p>我使用的分词工具是中科院计算所的开源版本的 <a href="http://ictclas.org/Down_OpenSrc.asp">ICTCLAS</a>. 另外还有开源的 <a href="http://code.google.com/p/nlpbamboo/">Bamboo</a>, 我随后也会对该工具进行调研.</p>
<p>从 ICTCLAS 出发是个不错的选择, 因为其算法传播比较广泛, 有公开的学术文档, 并且编译简单, 库依赖少. 但目前只提供了 C/C++, Java 和 C# 版本的代码, 并没有 PHP 版本的代码. 怎么办呢? 也许可以学习它的 C/C++ 源码和学术文档中, 然后再开发一个 PHP 版本出来. 不过, 我要使用进程间通信, 在 PHP 代码里调用 C/C++ 版本的可执行文件.</p>
<p><span id="more-495"></span>下载源码解压后, 在有 C++ 开发库和编译环境的机器上直接 make ictclas 即可. 它的 Makefile 脚本有个错误, 执行测试的代码没有加上&#8217;./&#8217;, 当然不能像 Windows 下执行成功了. 但也不影响编译结果.</p>
<p>进行中文分词的 PHP 类就在下面了, 用 proc_open() 函数来执行分词程序, 并通过管道和其交互, 输入要进行分词的文本, 读取分词结果.</p>
<pre>
&lt;?php
class NLP{
    private static $cmd_path;

    // 不以'/'结尾
    static function set_cmd_path($path){
        self::$cmd_path = $path;
    }

    private function cmd($str){
        $descriptorspec = array(
           0 =&gt; array("pipe", "r"),
           1 =&gt; array("pipe", "w"),
        );
        $cmd = self::$cmd_path . "/ictclas";
        $process = proc_open($cmd, $descriptorspec, $pipes);

        if (is_resource($process)) {
            $str = iconv('utf-8', 'gbk', $str);
            fwrite($pipes[0], $str);
            $output = stream_get_contents($pipes[1]);

            fclose($pipes[0]);
            fclose($pipes[1]);

            $return_value = proc_close($process);
        }

        /*
        $cmd = "printf '$input' | " . self::$cmd_path . "/ictclas";
        exec($cmd, $output, $ret);
        $output = join("\n", $output);
        */

        $output = trim($output);
        $output = iconv('gbk', 'utf-8', $output);

        return $output;
    }

    /**
     * 进行分词, 返回词语列表.
     */
    function tokenize($str){
        $tokens = array();

        $output = self::cmd($input);
        if($output){
            $ps = preg_split('/\s+/', $output);
            foreach($ps as $p){
                list($seg, $tag) = explode('/', $p);
                $item = array(
                    'seg' =&gt; $seg,
                    'tag' =&gt; $tag,
                    );
                $tokens[] = $item;
            }
        }

        return $tokens;
    }
}
NLP::set_cmd_path(dirname(__FILE__));
?&gt;
</pre>
<p>使用起来很简单(确保 ICTCLAS 编译后的可执行文件和词典在当前目录):</p>
<pre>
&lt;?php
require_once('NLP.php');
var_dump(NLP::tokenize('你好啊, 世界!'));
?&gt;
</pre>
<p>下一步将结合 ZendFramework 的 Zend_Search_Lucene 模块来做一个可以运行的中文搜索系统了!</p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/351.html' rel='bookmark' title='Permanent Link: 史上最强大的PHP MySQL操作类'>史上最强大的PHP MySQL操作类</a></li>
<li><a href='http://www.ideawu.net/blog/archives/320.html' rel='bookmark' title='Permanent Link: Zend Framework 的缓存模块 Zend_Cache 使用'>Zend Framework 的缓存模块 Zend_Cache 使用</a></li>
<li><a href='http://www.ideawu.net/blog/archives/202.html' rel='bookmark' title='Permanent Link: 写自己的 http_build_query'>写自己的 http_build_query</a></li>
<li><a href='http://www.ideawu.net/blog/archives/73.html' rel='bookmark' title='Permanent Link: Linux实用小工具 &#8212; 计算器wcalc'>Linux实用小工具 &#8212; 计算器wcalc</a></li>
<li><a href='http://www.ideawu.net/blog/archives/365.html' rel='bookmark' title='Permanent Link: C#环形缓冲'>C#环形缓冲</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/495.html" title="开发搜索引擎 &#8211; PHP中文分词">开发搜索引擎 &#8211; PHP中文分词</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>对于中文搜索引擎来说, 中文分词是整个系统最基础的部分之一, 因为目前基于单字的中文搜索算法并不是太好. 当然, 本文不是要对中文搜索引擎做研究, 而是分享如果用 PHP 做一个站内搜索引擎. 本文是这个系统中的一篇.</p>
<p>我使用的分词工具是中科院计算所的开源版本的 <a href="http://ictclas.org/Down_OpenSrc.asp">ICTCLAS</a>. 另外还有开源的 <a href="http://code.google.com/p/nlpbamboo/">Bamboo</a>, 我随后也会对该工具进行调研.</p>
<p>从 ICTCLAS 出发是个不错的选择, 因为其算法传播比较广泛, 有公开的学术文档, 并且编译简单, 库依赖少. 但目前只提供了 C/C++, Java 和 C# 版本的代码, 并没有 PHP 版本的代码. 怎么办呢? 也许可以学习它的 C/C++ 源码和学术文档中, 然后再开发一个 PHP 版本出来. 不过, 我要使用进程间通信, 在 PHP 代码里调用 C/C++ 版本的可执行文件.</p>
<p><span id="more-495"></span>下载源码解压后, 在有 C++ 开发库和编译环境的机器上直接 make ictclas 即可. 它的 Makefile 脚本有个错误, 执行测试的代码没有加上&#8217;./&#8217;, 当然不能像 Windows 下执行成功了. 但也不影响编译结果.</p>
<p>进行中文分词的 PHP 类就在下面了, 用 proc_open() 函数来执行分词程序, 并通过管道和其交互, 输入要进行分词的文本, 读取分词结果.</p>
<pre>
&lt;?php
class NLP{
    private static $cmd_path;

    // 不以'/'结尾
    static function set_cmd_path($path){
        self::$cmd_path = $path;
    }

    private function cmd($str){
        $descriptorspec = array(
           0 =&gt; array("pipe", "r"),
           1 =&gt; array("pipe", "w"),
        );
        $cmd = self::$cmd_path . "/ictclas";
        $process = proc_open($cmd, $descriptorspec, $pipes);

        if (is_resource($process)) {
            $str = iconv('utf-8', 'gbk', $str);
            fwrite($pipes[0], $str);
            $output = stream_get_contents($pipes[1]);

            fclose($pipes[0]);
            fclose($pipes[1]);

            $return_value = proc_close($process);
        }

        /*
        $cmd = "printf '$input' | " . self::$cmd_path . "/ictclas";
        exec($cmd, $output, $ret);
        $output = join("\n", $output);
        */

        $output = trim($output);
        $output = iconv('gbk', 'utf-8', $output);

        return $output;
    }

    /**
     * 进行分词, 返回词语列表.
     */
    function tokenize($str){
        $tokens = array();

        $output = self::cmd($input);
        if($output){
            $ps = preg_split('/\s+/', $output);
            foreach($ps as $p){
                list($seg, $tag) = explode('/', $p);
                $item = array(
                    'seg' =&gt; $seg,
                    'tag' =&gt; $tag,
                    );
                $tokens[] = $item;
            }
        }

        return $tokens;
    }
}
NLP::set_cmd_path(dirname(__FILE__));
?&gt;
</pre>
<p>使用起来很简单(确保 ICTCLAS 编译后的可执行文件和词典在当前目录):</p>
<pre>
&lt;?php
require_once('NLP.php');
var_dump(NLP::tokenize('你好啊, 世界!'));
?&gt;
</pre>
<p>下一步将结合 ZendFramework 的 Zend_Search_Lucene 模块来做一个可以运行的中文搜索系统了!</p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/351.html' rel='bookmark' title='Permanent Link: 史上最强大的PHP MySQL操作类'>史上最强大的PHP MySQL操作类</a></li>
<li><a href='http://www.ideawu.net/blog/archives/320.html' rel='bookmark' title='Permanent Link: Zend Framework 的缓存模块 Zend_Cache 使用'>Zend Framework 的缓存模块 Zend_Cache 使用</a></li>
<li><a href='http://www.ideawu.net/blog/archives/202.html' rel='bookmark' title='Permanent Link: 写自己的 http_build_query'>写自己的 http_build_query</a></li>
<li><a href='http://www.ideawu.net/blog/archives/73.html' rel='bookmark' title='Permanent Link: Linux实用小工具 &#8212; 计算器wcalc'>Linux实用小工具 &#8212; 计算器wcalc</a></li>
<li><a href='http://www.ideawu.net/blog/archives/365.html' rel='bookmark' title='Permanent Link: C#环形缓冲'>C#环形缓冲</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/495.html" title="开发搜索引擎 &#8211; PHP中文分词">开发搜索引擎 &#8211; PHP中文分词</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/495.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>链接包含&#8221;%2F&#8221;导致mod_rewrite失效</title>
		<link>http://www.ideawu.net/blog/archives/494.html</link>
		<comments>http://www.ideawu.net/blog/archives/494.html#comments</comments>
		<pubDate>Wed, 05 May 2010 02:05:09 +0000</pubDate>
		<dc:creator>ideawu</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[mod_rewrite]]></category>

		<guid isPermaLink="false">http://www.ideawu.net/blog/archives/494.html</guid>
		<description><![CDATA[<p>IT牛人博客聚合网站(<a href="http://www.udpwork.com/">www.udpwork.com</a>)用到了 Apache 的 mod_rewrite 模块进行 URL 重写. 但是, 在使用过程中曾经出现过一个比较诡异的问题. 开始认为是重写规则设置得不对, 后来才发现, 是&#8221;%2F&#8221;导致 Apache 直接返回 404 错误.</p>
<p>比如浏览查看某个标签下的文章列表的链接为</p>
<pre>

http://www.udpwork.com/tag/Linux
</pre>
<p>在重写之前的链接是</p>
<pre>

http://www.udpwork.com/?tag=Linux
</pre>
<p><span id="more-494"></span></p>
<p>在 <a href="http://www.ideawu.net/blog/category/web/php">PHP</a> 脚本中用如下代码重写 URL</p>
<pre>
$url = '/tag/' . urlencode($tag);
</pre>
<p>相应的 Apache mod_rewrite 配置为</p>
<pre>
RewriteCond  %{REQUEST_URI}  ^/tag/.*$
RewriteRule  ^/tag/(.*)$  ?tag=$1  [L]
</pre>
<p>不过, 当 tag 中包含斜杠&#8217;/'时, 出现了问题, 服务器提示&#8221;404 &#8211; Not Found&#8221;. 比如 tag 是 Unix/Linux, 生成的链接是</p>
<pre>

http://www.udpwork.com/tag/Unix%2FLinux
</pre>
<p>斜杠&#8217;/'被转义成&#8217;%2F&#8217;, 那么最终的还原后的链接应该是</p>
<pre>

http://www.udpwork.com/?tag=Unix%2FLinux
</pre>
<p>直接访问这个未重写过的 URL, 是完全正常的. 但 Apache 一直报 404 错误. 后来才发现, 原来 Apache 有一个配置项&#8221;<a href="http://httpd.apache.org/docs/2.0/mod/core.html#allowencodedslashes">AllowEncodedSlashes</a>&#8220;, 默认是&#8221;Off&#8221;, 也就是不允许请求路径(上例是 /tag/Unix%2FLinux)中包含编码后的斜杠&#8217;/'(在某些平台是反斜杠&#8217;\'). 这个选项的的相应代码在 mod_rewrite 模块被执行之前</p>
<pre>
// request.c
AP_DECLARE(int) ap_process_request_internal(request_rec *r){
	if (d->allow_encoded_slashes) {
		access_status = ap_unescape_url_keep2f(r->parsed_uri.path);
	} else {
		access_status = ap_unescape_url(r->parsed_uri.path);
	}
}

// util.c
AP_DECLARE(int) ap_unescape_url(char *url){
	if (IS_SLASH(*x) || *x == '\0')
		badpath = 1;
	...
	else if (badpath)
		return HTTP_NOT_FOUND;
}
</pre>
<p>默认不允许包含&#8221;%2F&#8221;. 如果请求路径中包含了, 那么 ap_unescape_url() 函数认为是无效的路径, 直接返回 HTTP_NOT_FOUND, 最终浏览器得到的是&#8221;404 &#8211; Not Found&#8221;出错页面. 当然可以通过修改 Apache 配置来解决这个问题, 不过, 在 PHP 脚本中解决更通用</p>
<pre>
$url = '/tag/' . urlencode(str_replace('/', '%2F', $tag));
</pre>
<p>这样, 斜杠被两次转义, 变为&#8221;/tag/Unix%252FLinux&#8221;. Apache 接收到请求后, 进行一次解码, 得到&#8221;/tag/Unix%2FLinux&#8221;, 以参数&#8221;tag=Unix%2FLinux&#8221;交给 PHP 脚本处理, PHP 自己再将请求参数进行一次解码, $_GET['tag'] 的值就是&#8221;Unix/Linux&#8221;了.</p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/618.html' rel='bookmark' title='Permanent Link: Apache用mod_rewrite配置子域名'>Apache用mod_rewrite配置子域名</a></li>
<li><a href='http://www.ideawu.net/blog/archives/460.html' rel='bookmark' title='Permanent Link: 我为什么要放弃订阅转而做牛人博客聚合'>我为什么要放弃订阅转而做牛人博客聚合</a></li>
<li><a href='http://www.ideawu.net/blog/archives/214.html' rel='bookmark' title='Permanent Link: Web设计与开发服务'>Web设计与开发服务</a></li>
<li><a href='http://www.ideawu.net/blog/archives/539.html' rel='bookmark' title='Permanent Link: 调查: 你认为IT牛人博客聚合网站应该提供RSS吗?'>调查: 你认为IT牛人博客聚合网站应该提供RSS吗?</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/494.html" title="链接包含&#8221;%2F&#8221;导致mod_rewrite失效">链接包含&#8221;%2F&#8221;导致mod_rewrite失效</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></description>
			<content:encoded><![CDATA[<p>IT牛人博客聚合网站(<a href="http://www.udpwork.com/">www.udpwork.com</a>)用到了 Apache 的 mod_rewrite 模块进行 URL 重写. 但是, 在使用过程中曾经出现过一个比较诡异的问题. 开始认为是重写规则设置得不对, 后来才发现, 是&#8221;%2F&#8221;导致 Apache 直接返回 404 错误.</p>
<p>比如浏览查看某个标签下的文章列表的链接为</p>
<pre>

http://www.udpwork.com/tag/Linux
</pre>
<p>在重写之前的链接是</p>
<pre>

http://www.udpwork.com/?tag=Linux
</pre>
<p><span id="more-494"></span></p>
<p>在 <a href="http://www.ideawu.net/blog/category/web/php">PHP</a> 脚本中用如下代码重写 URL</p>
<pre>
$url = '/tag/' . urlencode($tag);
</pre>
<p>相应的 Apache mod_rewrite 配置为</p>
<pre>
RewriteCond  %{REQUEST_URI}  ^/tag/.*$
RewriteRule  ^/tag/(.*)$  ?tag=$1  [L]
</pre>
<p>不过, 当 tag 中包含斜杠&#8217;/'时, 出现了问题, 服务器提示&#8221;404 &#8211; Not Found&#8221;. 比如 tag 是 Unix/Linux, 生成的链接是</p>
<pre>

http://www.udpwork.com/tag/Unix%2FLinux
</pre>
<p>斜杠&#8217;/'被转义成&#8217;%2F&#8217;, 那么最终的还原后的链接应该是</p>
<pre>

http://www.udpwork.com/?tag=Unix%2FLinux
</pre>
<p>直接访问这个未重写过的 URL, 是完全正常的. 但 Apache 一直报 404 错误. 后来才发现, 原来 Apache 有一个配置项&#8221;<a href="http://httpd.apache.org/docs/2.0/mod/core.html#allowencodedslashes">AllowEncodedSlashes</a>&#8220;, 默认是&#8221;Off&#8221;, 也就是不允许请求路径(上例是 /tag/Unix%2FLinux)中包含编码后的斜杠&#8217;/'(在某些平台是反斜杠&#8217;\'). 这个选项的的相应代码在 mod_rewrite 模块被执行之前</p>
<pre>
// request.c
AP_DECLARE(int) ap_process_request_internal(request_rec *r){
	if (d->allow_encoded_slashes) {
		access_status = ap_unescape_url_keep2f(r->parsed_uri.path);
	} else {
		access_status = ap_unescape_url(r->parsed_uri.path);
	}
}

// util.c
AP_DECLARE(int) ap_unescape_url(char *url){
	if (IS_SLASH(*x) || *x == '\0')
		badpath = 1;
	...
	else if (badpath)
		return HTTP_NOT_FOUND;
}
</pre>
<p>默认不允许包含&#8221;%2F&#8221;. 如果请求路径中包含了, 那么 ap_unescape_url() 函数认为是无效的路径, 直接返回 HTTP_NOT_FOUND, 最终浏览器得到的是&#8221;404 &#8211; Not Found&#8221;出错页面. 当然可以通过修改 Apache 配置来解决这个问题, 不过, 在 PHP 脚本中解决更通用</p>
<pre>
$url = '/tag/' . urlencode(str_replace('/', '%2F', $tag));
</pre>
<p>这样, 斜杠被两次转义, 变为&#8221;/tag/Unix%252FLinux&#8221;. Apache 接收到请求后, 进行一次解码, 得到&#8221;/tag/Unix%2FLinux&#8221;, 以参数&#8221;tag=Unix%2FLinux&#8221;交给 PHP 脚本处理, PHP 自己再将请求参数进行一次解码, $_GET['tag'] 的值就是&#8221;Unix/Linux&#8221;了.</p>


<h3>Related posts:</h3><ol><li><a href='http://www.ideawu.net/blog/archives/618.html' rel='bookmark' title='Permanent Link: Apache用mod_rewrite配置子域名'>Apache用mod_rewrite配置子域名</a></li>
<li><a href='http://www.ideawu.net/blog/archives/460.html' rel='bookmark' title='Permanent Link: 我为什么要放弃订阅转而做牛人博客聚合'>我为什么要放弃订阅转而做牛人博客聚合</a></li>
<li><a href='http://www.ideawu.net/blog/archives/214.html' rel='bookmark' title='Permanent Link: Web设计与开发服务'>Web设计与开发服务</a></li>
<li><a href='http://www.ideawu.net/blog/archives/539.html' rel='bookmark' title='Permanent Link: 调查: 你认为IT牛人博客聚合网站应该提供RSS吗?'>调查: 你认为IT牛人博客聚合网站应该提供RSS吗?</a></li>
</ol><div><p><img src="http://www.ideawu.net/favicon.ico" /> 你现在看的文章是: <a href="http://www.ideawu.net/blog/archives/494.html" title="链接包含&#8221;%2F&#8221;导致mod_rewrite失效">链接包含&#8221;%2F&#8221;导致mod_rewrite失效</a></p>

<div>
	<a href="http://www.benegg.com/linode-ad.php">
		Linode VPS - 美国虚拟主机
	</a>
	|
	<a href="http://www.udpwork.com/">
		IT牛人博客聚合网站
	</a>
</div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ideawu.net/blog/archives/494.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

