<?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>webfuns--趣味互联网</title>
	<atom:link href="http://blog.webfuns.net/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.webfuns.net</link>
	<description>趣味互联网-关注互联网的趣味话题：LAMP(Linux-Apache-MySQL-PHP)、Google、Wordpress等   作者：tomheng</description>
	<lastBuildDate>Thu, 19 Jan 2012 05:36:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>DNS预获取（dns-prefetch）</title>
		<link>http://blog.webfuns.net/archives/1357.html</link>
		<comments>http://blog.webfuns.net/archives/1357.html#comments</comments>
		<pubDate>Mon, 16 Jan 2012 14:02:08 +0000</pubDate>
		<dc:creator>tomheng</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[DNS]]></category>

		<guid isPermaLink="false">http://blog.webfuns.net/?p=1357</guid>
		<description><![CDATA[今天翻看twitter的源码的时候看到了一下内容： &#60;link rel=”dns-prefetch” href=”http://a0.twimg.com”/&#62; &#60;link rel=”dns-prefetch” href=”http://a1.twimg.com”/&#62; &#60;link rel=”dns-prefetch” href=”http://a2.twimg.com”/&#62; &#60;link rel=”dns-prefetch” href=”http://a3.twimg.com”/&#62; &#60;link rel=”dns-prefetch” href=”http://api.twitter.com”/&#62; 查阅了相关资料，知道DNS Prefetch也就是DNS预获取，也是前段优化的一部分。在前段优化中关于DNS的有两点：一是减少DNS的请求次数，第二个就是进行DNS预先获取。 DNS Prefetch 已经被下面的浏览器支持 Firefox: 3.5+ Chrome: Supported Safari 5+ Opera: Unknown IE: 9 (called “Pre-resolution” on blogs.msdn.com) 默认情况下浏览器会对页面中和当前域名（正在浏览网页的域名）不在同一个域的域名进行预获取，并且缓存结果，这就是隐式的DNS Prefetch。如果想对页面中没有出现的域进行预获取，那么就要使用显示的DNS　Prefetch了，也就是使用link标签： &#60;link rel=”dns-prefetch” href=”http://api.twitter.com”/&#62; DNS Prefetch应该尽量的放在网页的前面，推荐放在&#60;meta charset=”/&#62;后面。 PS:可以通过下面的标签禁止隐式的DNS Prefetch。 &#60;meta http-equiv=”x-dns-prefetch-control” content=”off”&#62; 内容整理自：DNS-Prefetching 参考： 网站优化应重视 DNS 预获取(DNS Prefetching) 评论&#124; [...]]]></description>
			<content:encoded><![CDATA[<p>今天翻看twitter的源码的时候看到了一下内容：</p>
<p><span style="color: #0000ff;">&lt;link rel=”dns-prefetch” href=”http://a0.twimg.com”/&gt;</span></p>
<p><span style="color: #0000ff;">&lt;link rel=”dns-prefetch” href=”http://a1.twimg.com”/&gt;</span></p>
<p><span style="color: #0000ff;">&lt;link rel=”dns-prefetch” href=”http://a2.twimg.com”/&gt;</span></p>
<p><span style="color: #0000ff;">&lt;link rel=”dns-prefetch” href=”http://a3.twimg.com”/&gt;</span></p>
<p><span style="color: #0000ff;">&lt;link rel=”dns-prefetch” href=”http://api.twitter.com”/&gt;</span></p>
<p>查阅了相关资料，知道DNS Prefetch也就是DNS预获取，也是前段优化的一部分。在前段优化中关于DNS的有两点：一是减少DNS的请求次数，第二个就是进行DNS预先获取。</p>
<p>DNS Prefetch 已经被下面的浏览器支持</p>
<ul>
<li>Firefox: 3.5+</li>
<li>Chrome: Supported</li>
<li>Safari 5+</li>
<li>Opera: Unknown</li>
<li>IE: 9 (called “Pre-resolution” on blogs.msdn.com)</li>
</ul>
<p>默认情况下浏览器会对页面中和当前域名（正在浏览网页的域名）不在同一个域的域名进行预获取，并且缓存结果，这就是隐式的DNS Prefetch。如果想对页面中没有出现的域进行预获取，那么就要使用显示的DNS　Prefetch了，也就是使用link标签：</p>
<p><span style="color: #0000ff;">&lt;link rel=”dns-prefetch” href=”http://api.twitter.com”/&gt;</span></p>
<p>DNS Prefetch应该尽量的放在网页的前面，推荐放在&lt;meta charset=”/&gt;后面。</p>
<p>PS:可以通过下面的标签禁止隐式的DNS Prefetch。<br />
<span style="color: #0000ff;">&lt;meta http-equiv=”x-dns-prefetch-control” content=”off”&gt;</span></p>
<p>内容整理自：<a href="https://github.com/h5bp/html5-boilerplate/wiki/DNS-Prefetching">DNS-Prefetching</a></p>
<p>参考：<br />
<a href="http://www.dbanotes.net/web/dns_prefetching.html">网站优化应重视 DNS 预获取(DNS Prefetching)</a></p>
<hr/><small><a href="http://blog.webfuns.net/archives/1357.html#comments" title="评论">评论</a>| 作者: <a href="http://blog.webfuns.net" >tomheng</a> 85159e08d5f1f50d244825cb03e92c65</small>]]></content:encoded>
			<wfw:commentRss>http://blog.webfuns.net/archives/1357.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>如何设置一个严格30分钟过期的Session</title>
		<link>http://blog.webfuns.net/archives/1329.html</link>
		<comments>http://blog.webfuns.net/archives/1329.html#comments</comments>
		<pubDate>Tue, 10 Jan 2012 15:35:45 +0000</pubDate>
		<dc:creator>tomheng</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[PHP基础]]></category>

		<guid isPermaLink="false">http://blog.webfuns.net/?p=1329</guid>
		<description><![CDATA[问题是Laruence在微博上提出的: 我在面试的时候, 经常会问一个问题: “如何设置一个30分钟过期的Session?”, 大家不要觉得看似简单, 这里面包含的知识多着呢, 特别适合考察基本功是否扎实, 谁来回答试试? 呵呵 说实话，如果是面试我的话，我回答不好。我找了一下stackoverflow发现有人讨论过同样的问题，How do I expire a PHP session after 30 minutes?，看了一下Gumbo回复应该是最好的。过了不久Laruence也整理了一篇文章，讲的更全面一些。 Gumbo说的对，要回到好这个问题只要弄清楚session.gc_maxlifetime和session.cookie_lifetime这两个配置的原理就可以了。 session.gc_maxlifetime这个时间是指的Session过多少时间会被session GC回收。这里需要主要以下几点 GC启动本身是有概率的（ session.gc_probability和session.gc_divisor） 不同的脚本具有不同的 session.gc_maxlifetime 数值但是共享了同一个地方存储会话数据，则具有最小数值的脚本会清理数据。此情况下，与 session.save_path 一起使用本指令 GC和这个时间比较的时候是用的sesssion文件的mtime（ &#62;PHP 4.2.3）而不是atime session.cookie_lifetime 这个是Server发送给浏览器的cookie过期时间 所以只要明白了这两个配置的原理，那么这个问题至少会回答及格（我认为）。 所以手册很重要，不管是哪种技术，如果把手册研究一篇，搞懂80%以上，那么不是大牛也是个高级工程师鸟。 &#160; 评论&#124; 作者: tomheng 85159e08d5f1f50d244825cb03e92c65]]></description>
			<content:encoded><![CDATA[<p>问题是<a href="http://t.sina.com.cn/laruence">Laruence</a>在微博上提出的:</p>
<blockquote><p>我在面试的时候, 经常会问一个问题: “如何设置一个30分钟过期的Session?”, 大家不要觉得看似简单, 这里面包含的知识多着呢, 特别适合考察基本功是否扎实, 谁来回答试试? 呵呵</p></blockquote>
<p>说实话，如果是面试我的话，我回答不好。我找了一下stackoverflow发现有人讨论过同样的问题，<a href="http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes">How do I expire a PHP session after 30 minutes?</a>，看了一下<a href="http://stackoverflow.com/users/53114/gumbo">Gumbo</a>回复应该是最好的。过了不久Laruence也整理了一篇<a href="http://www.laruence.com/2012/01/10/2469.html">文章</a>，讲的更全面一些。 Gumbo说的对，要回到好这个问题只要弄清楚<strong><a href="http://php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime">session.gc_maxlifetime</a></strong>和<strong><strong><a href="http://php.net/manual/en/session.configuration.php#ini.session.cookie-lifetime">session.cookie_lifetime</a></strong></strong>这两个配置的原理就可以了。 <strong>session.gc_maxlifetime</strong>这个时间是指的Session过多少时间会被session GC回收。这里需要主要以下几点</p>
<ul>
<li>GC启动本身是有概率的（ <a href="http://www.php.net/manual/en/session.configuration.php#ini.session.gc-probability">session.gc_probability</a>和<a href="http://www.php.net/manual/en/session.configuration.php#ini.session.gc-divisor">session.gc_divisor</a>）</li>
<li>不同的脚本具有不同的 session.gc_maxlifetime 数值但是共享了同一个地方存储会话数据，则具有最小数值的脚本会清理数据。此情况下，与 session.save_path 一起使用本指令</li>
<li>GC和这个时间比较的时候是用的sesssion文件的mtime（ &gt;PHP 4.2.3）而不是atime</li>
</ul>
<p><strong>session.cookie_lifetime</strong></p>
<p>这个是Server发送给浏览器的cookie过期时间</p>
<p>所以只要明白了这两个配置的原理，那么这个问题至少会回答及格（我认为）。</p>
<p>所以手册很重要，不管是哪种技术，如果把手册研究一篇，搞懂80%以上，那么不是大牛也是个高级工程师鸟。</p>
<p>&nbsp;</p>
<hr/><small><a href="http://blog.webfuns.net/archives/1329.html#comments" title="评论">评论</a>| 作者: <a href="http://blog.webfuns.net" >tomheng</a> 85159e08d5f1f50d244825cb03e92c65</small>]]></content:encoded>
			<wfw:commentRss>http://blog.webfuns.net/archives/1329.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL中得到当前使用的数据库</title>
		<link>http://blog.webfuns.net/archives/1320.html</link>
		<comments>http://blog.webfuns.net/archives/1320.html#comments</comments>
		<pubDate>Thu, 05 Jan 2012 12:38:06 +0000</pubDate>
		<dc:creator>tomheng</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.webfuns.net/?p=1320</guid>
		<description><![CDATA[在MySQL中用use 切换使用的数据库（或者在PHP中用mysql_select_db函数)，反过来我们怎样知道正在使用的是哪个数据库呢？ 这种需求很少遇到，但是还是会遇到的：） 所以找了一下，发现MySQL可以通过select databse()命令来查看当前使用的数据库。 ps:MySQL database函数 推荐：Stop Making Apps 再搭车推荐： 《他们在毕业的前一天爆炸 》《听说》《那些年，我们一起追的女孩》值得一看 评论&#124; 作者: tomheng 85159e08d5f1f50d244825cb03e92c65]]></description>
			<content:encoded><![CDATA[<p>在MySQL中用use 切换使用的数据库（或者在PHP中用mysql_select_db函数)，反过来我们怎样知道正在使用的是哪个数据库呢？</p>
<p>这种需求很少遇到，但是还是会遇到的：）</p>
<p>所以找了一下，发现MySQL可以通过<span style="color: #0000ff;">select databse()</span>命令来查看当前使用的数据库。</p>
<p>ps:<a href="http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_database">MySQL database函数</a></p>
<p>推荐：<a href="http://techcrunch.com/2011/11/11/start-making-sense/?utm_source=feedburner&amp;utm_medium=feed&amp;utm_campaign=Feed%3A+Techcrunch+%28TechCrunch%29">Stop Making Apps</a></p>
<p>再搭车推荐：</p>
<p><em>《他们在毕业的前一天爆炸 》《听说》《那些年，我们一起追的女孩》值得一看</em></p>
<hr/><small><a href="http://blog.webfuns.net/archives/1320.html#comments" title="评论">评论</a>| 作者: <a href="http://blog.webfuns.net" >tomheng</a> 85159e08d5f1f50d244825cb03e92c65</small>]]></content:encoded>
			<wfw:commentRss>http://blog.webfuns.net/archives/1320.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>让2012来的更猛烈些吧</title>
		<link>http://blog.webfuns.net/archives/1290.html</link>
		<comments>http://blog.webfuns.net/archives/1290.html#comments</comments>
		<pubDate>Sat, 31 Dec 2011 15:59:45 +0000</pubDate>
		<dc:creator>tomheng</dc:creator>
				<category><![CDATA[快乐生活]]></category>
		<category><![CDATA[总结]]></category>

		<guid isPermaLink="false">http://blog.webfuns.net/?p=1290</guid>
		<description><![CDATA[好吧，2012真的要来了！ 除了上面一句，一下内容算流水账一类的内容了。 对我而言，2011年如果用一个词来描述的话，我想应该是‘初来乍到’，最大的感受就是“不容易”，关键词是“实习、毕业、父母、工作”。 2011年的时间线 2011年1月8日，考完试我就来到了北京想找一个实习机会。因为大四下学期基本上没有什么课程了，主要就是实习，考试前已经有同学去寻找实习机会了。毕竟是初生牛犊，来北京是慎重考虑的结果，而且做好了各种准备，准备了两个月左右的生活费（来之前问过一些在北京的朋友，一个月生活费，省着点的话大概2000左右），联系了一些在北京的朋友必要时提供一些帮助。来到之后，随便找了一个房子（其实是地下室吧），买了些生活必需品，之后的事情就是找工作了。经历了多次面试之后，最后决定去猛买实习，其实在猛买不像是实习，更像是正式工作，学到了很多东西。因为自己初来乍到也犯过很多错误。 2011年5月下旬，因为最后的毕业答辩要回到学校才行，随启程返回烟台。之后一个月就是各种搞论文、各种聚会、各种玩了，那是另外一个故事了，一直想写一下最后的那一个月的生活，以后有可能再补上吧！ 2011年6月10日，处理完学校的事情之后回到北京，来之前我已经给张总发了一封离职邮件说明一下。所以接下来又要重新找工作，这次面试的都是一些稍大些的公司，经过一月左右的尝试，最后选择了金鹰网（2011年7月12日）。 接下来是两个月安稳时期，应是在8月初，部门经理跟我说我们部门要搬到长沙去，金鹰网北京这边应该算是一个办事处，大部队是在长沙那边，当时我的同意去长沙，毕竟年多走一些地方，长长见识是好的。 于是乎8月下旬迁往长沙，到长沙不到一周，我姐打来电话说我爸得了食道癌，当时听了之后，我一时真不知道应该怎么办了，下午跟部门经理请了两周的假。我爸得病的事突如其来，对我的打击很大，当时回济南以后就是安排老人到济南做手术，医生说如果不做手术两三个月后就不能吃东西了（其实当时吞咽已经有困难了），之后20多天就是在医院照顾父亲，值得庆幸的是父亲手术很成功。在 那20天了哭了很多次，想了很多的事情，有很多感触。 2011年10月4日回到长沙，基于当时父亲的情况，我不想走远了。和部门经理商量之后准备回北京。 2011年10月中旬再次回到北京。 毕业第一年走的不是很顺利，但这样已经很好了。 感受 无力，这一年我感到前所未有的无力感，太弱小，很多东西无法改变（也是我不够努力）。 人生最重要的是健康快乐，对于我们做IT的人更是如此，要多锻炼，少熬夜！ 坚持真的很重要，永远不要放弃。 学海无涯，技术、生活、感情各个方面都有学不完的知识 收获 对自己的认识深了一层，知道自己真正想要什么东西了，其实内心一直知道自己想要什么，只不过自己不知道。 买了kindle，读书多了起来。 2012年 希望爸妈、姐、侄女可以健健康康、平平安安！ 自己可以有所作为 走更多的地方，认识更多的朋友 可以赚更多的钱 脱光 玛雅预言失败 附： 2009总结 评论&#124; 作者: tomheng 85159e08d5f1f50d244825cb03e92c65]]></description>
			<content:encoded><![CDATA[<p>好吧，2012真的要来了！</p>
<p><span style="color: #ff0000;">除了上面一句，一下内容算流水账一类的内容了。</span></p>
<p>对我而言，2011年如果用一个词来描述的话，我想应该是‘初来乍到’，最大的感受就是“不容易”，关键词是“实习、毕业、父母、工作”。</p>
<p><strong>2011年的时间线</strong></p>
<p>2011年1月8日，考完试我就来到了北京想找一个实习机会。因为大四下学期基本上没有什么课程了，主要就是实习，考试前已经有同学去寻找实习机会了。毕竟是初生牛犊，来北京是慎重考虑的结果，而且做好了各种准备，准备了两个月左右的生活费（来之前问过一些在北京的朋友，一个月生活费，省着点的话大概2000左右），联系了一些在北京的朋友必要时提供一些帮助。来到之后，随便找了一个房子（其实是地下室吧），买了些生活必需品，之后的事情就是找工作了。经历了多次面试之后，最后决定去<a href="http://www.mengmai.com">猛买</a>实习，其实在猛买不像是实习，更像是正式工作，学到了很多东西。因为自己初来乍到也犯过很多错误。</p>
<p>2011年5月下旬，因为最后的毕业答辩要回到学校才行，随启程返回烟台。之后一个月就是各种搞论文、各种聚会、各种玩了，那是另外一个故事了，一直想写一下最后的那一个月的生活，以后有可能再补上吧！</p>
<p>2011年6月10日，处理完学校的事情之后回到北京，来之前我已经给<a href="http://www.zyzhang.com">张总</a>发了一封离职邮件说明一下。所以接下来又要重新找工作，这次面试的都是一些稍大些的公司，经过一月左右的尝试，最后选择了<a href="http://www.hunantv.com">金鹰网</a>（2011年7月12日）。</p>
<p>接下来是两个月安稳时期，应是在8月初，部门经理跟我说我们部门要搬到长沙去，金鹰网北京这边应该算是一个办事处，大部队是在长沙那边，当时我的同意去长沙，毕竟年多走一些地方，长长见识是好的。</p>
<p>于是乎8月下旬迁往长沙，到长沙不到一周，我姐打来电话说我爸得了食道癌，当时听了之后，我一时真不知道应该怎么办了，下午跟部门经理请了两周的假。我爸得病的事突如其来，对我的打击很大，当时回济南以后就是安排老人到济南做手术，医生说如果不做手术两三个月后就不能吃东西了（其实当时吞咽已经有困难了），之后20多天就是在医院照顾父亲，值得庆幸的是父亲手术很成功。在 那20天了哭了很多次，想了很多的事情，有很多感触。</p>
<p>2011年10月4日回到长沙，基于当时父亲的情况，我不想走远了。和部门经理商量之后准备回北京。</p>
<p>2011年10月中旬再次回到北京。</p>
<p>毕业第一年走的不是很顺利，但这样已经很好了。</p>
<p><strong>感受</strong></p>
<ul>
<li>无力，这一年我感到前所未有的无力感，太弱小，很多东西无法改变（也是我不够努力）。</li>
<li>人生最重要的是健康快乐，对于我们做IT的人更是如此，要多锻炼，少熬夜！</li>
<li>坚持真的很重要，永远不要放弃。</li>
<li>学海无涯，技术、生活、感情各个方面都有学不完的知识</li>
</ul>
<p><strong>收获</strong></p>
<p>对自己的认识深了一层，知道自己真正想要什么东西了，其实内心一直知道自己想要什么，只不过自己不知道。</p>
<p>买了kindle，读书多了起来。</p>
<p><strong>2012年</strong></p>
<ul>
<li>希望爸妈、姐、侄女可以健健康康、平平安安！</li>
<li>自己可以有所作为</li>
<li>走更多的地方，认识更多的朋友</li>
<li>可以赚更多的钱</li>
<li>脱光</li>
<li>玛雅预言失败</li>
</ul>
<p>附：<br />
<a title="回顾09展望10" href="http://blog.webfuns.net/archives/414.html">2009总结</a></p>
<hr/><small><a href="http://blog.webfuns.net/archives/1290.html#comments" title="评论">评论</a>| 作者: <a href="http://blog.webfuns.net" >tomheng</a> 85159e08d5f1f50d244825cb03e92c65</small>]]></content:encoded>
			<wfw:commentRss>http://blog.webfuns.net/archives/1290.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>关于CSDN用户数据泄露的一些思考</title>
		<link>http://blog.webfuns.net/archives/1259.html</link>
		<comments>http://blog.webfuns.net/archives/1259.html#comments</comments>
		<pubDate>Thu, 22 Dec 2011 12:25:14 +0000</pubDate>
		<dc:creator>tomheng</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[CSDN]]></category>
		<category><![CDATA[安全]]></category>

		<guid isPermaLink="false">http://blog.webfuns.net/?p=1259</guid>
		<description><![CDATA[“有很多用户的密码在网上飘荡，而且它还是明文的” CSDN 600万的用户信息（用户名、密码和邮箱）泄露了，而且密码是明文存储的。之后又爆出了人人、多玩等众多网站的用户信息也被泄露了。当然这只是黑客门放出的一部分信息，只是冰山一角，不过管中窥豹可以知道现在互联网是多么的不安全，多么的没有隐私，翻开手机看看手机里的那些垃圾短息吧！还有接到的那些很多莫名其妙的电话。 毋庸置疑互联网时代安全是一个大问题，但是互联网的安全性和开放性始终一对矛盾，在一个开发的系统中想实现绝对的安全似乎是不可能的。在2011年的尾巴上的这次信息泄露事件，从另一侧面来看反而是一件好事，无论对于网站开发人员还是最终用户，大家的安全意识应该在这次事件中有一个提高。 如何保证信息的安全? (1) 加密算法的选择 MD5、SHA、Bcrypt() 、PHK、SRP等究竟应该选择哪一个算法比较安全。这里做了一个关于Bcrypt 、PHK、SRP的讨论，作者指出一个越快的加密算法越不安全，快即意味着破解或者攻击所需要的时间也会减少，所以选择加密算法加密速度是一个考虑的因素，作者最后推荐的加密算法是Bcrypt。 牛逼、并且最靠谱的方法可能是自己开发一个加密算法、当然开发的算法强度至少要达到MD5，然后要严格保密这种加密算法（这又是个问题），那么被破解的概率就要小很多了。 还有一点就是选择常用的加密方法，虽然是一个笨得思路，但也许能起到一些作用。毕竟现在最完备的字典应该是MD5的，如果采用非MD5的加密方法，那么被字典攻击的时间消耗就要更长一些。 更多关于加密算法选择的讨论： (PHP) SHA1 vs md5 vs SHA256: which to use for a PHP login? How To Safely Store A Password （2）如何加密 对于用户密码来说一般都是单向加密，加密时一定要加salt，而且要每一个用户一个salt，这样就会大大的增加字典破解的难度，如果选用MD5这种快速加密方法，推荐至少加密两次。 (3)强制用户使用复杂密码，这一招也很有用 用户应该怎样管理自己的密码 2009年的时候写过一篇关于密码管理的文章，这里不再重复了。 关于安全问题的讨论 How do you use bcrypt for hashing passwords in PHP? 谈谈近期的安全事件 相关网站 OWASP WooYun PS: 今天又发现天涯4000万的用户数据，又是明文存储。不过事件的发展越来越阴谋论了，互联网在这里真好玩。（update: 2011-12-25) [...]]]></description>
			<content:encoded><![CDATA[<p>“有很多用户的密码在网上飘荡，而且它还是明文的”</p>
<p>CSDN 600万的用户信息（用户名、密码和邮箱）泄露了，而且密码是明文存储的。之后又爆出了人人、多玩等众多网站的用户信息也被泄露了。当然这只是黑客门放出的一部分信息，只是冰山一角，不过管中窥豹可以知道现在互联网是多么的不安全，多么的没有隐私，翻开手机看看手机里的那些垃圾短息吧！还有接到的那些很多莫名其妙的电话。</p>
<p>毋庸置疑互联网时代安全是一个大问题，但是互联网的安全性和开放性始终一对矛盾，在一个开发的系统中想实现绝对的安全似乎是不可能的。在2011年的尾巴上的这次信息泄露事件，从另一侧面来看反而是一件好事，无论对于网站开发人员还是最终用户，大家的安全意识应该在这次事件中有一个提高。</p>
<p><strong>如何保证信息的安全?</strong></p>
<p>(1) 加密算法的选择</p>
<p><a href="http://zh.wikipedia.org/wiki/MD5#Security">MD5</a>、<a href="http://zh.wikipedia.org/wiki/SHA%E5%AE%B6%E6%97%8F">SHA</a>、<a href="http://coolshell.cn/articles/2078.html">Bcrypt() </a>、PHK、SRP等究竟应该选择哪一个算法比较安全。<a title="Enough With The Rainbow Tables: What You Need To Know About Secure Password Schemes" href="http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html">这里</a>做了一个关于Bcrypt 、PHK、SRP的讨论，作者指出一个越快的加密算法越不安全，快即意味着破解或者攻击所需要的时间也会减少，所以选择加密算法加密速度是一个考虑的因素，作者最后推荐的加密算法是Bcrypt。</p>
<p>牛逼、并且最靠谱的方法可能是自己开发一个加密算法、当然开发的算法强度至少要达到MD5，然后要严格保密这种加密算法（这又是个问题），那么被破解的概率就要小很多了。</p>
<p>还有一点就是选择常用的加密方法，虽然是一个笨得思路，但也许能起到一些作用。毕竟现在最完备的字典应该是MD5的，如果采用非MD5的加密方法，那么被字典攻击的时间消耗就要更长一些。</p>
<p>更多关于加密算法选择的讨论：</p>
<ul>
<li><a href="http://stackoverflow.com/questions/2235158/php-sha1-vs-md5-vs-sha256-which-to-use-for-a-php-login">(PHP) SHA1 vs md5 vs SHA256: which to use for a PHP login?</a></li>
<li><a href="http://codahale.com/how-to-safely-store-a-password/">How To Safely Store A Password</a></li>
</ul>
<p>（2）如何加密</p>
<p>对于用户密码来说一般都是单向加密，加密时一定要加salt，而且要每一个用户一个salt，这样就会大大的增加字典破解的难度，如果选用MD5这种快速加密方法，推荐至少加密两次。</p>
<p>(3)强制用户使用复杂密码，这一招也很有用</p>
<p><strong>用户应该怎样管理自己的密码</strong></p>
<p>2009年的时候写过一篇关于<a href="http://blog.webfuns.net/archives/409.html">密码管理</a>的文章，这里不再重复了。</p>
<p><strong>关于安全问题的讨论</strong></p>
<ul>
<li><a href="http://stackoverflow.com/questions/4795385/how-do-you-use-bcrypt-for-hashing-passwords-in-php">How do you use bcrypt for hashing passwords in PHP?</a></li>
<li><a href="http://hi.baidu.com/caoz/blog/item/edcc36d3f812891e3af3cf28.html">谈谈近期的安全事件</a></li>
</ul>
<div><strong>相关网站</strong></div>
<div>
<ul>
<li><a title="OWASP Guide Project" href="https://www.owasp.org/index.php/Category:OWASP_Guide_Project">OWASP</a></li>
<li><a title="自由平等的漏洞报告平台" href="http://www.wooyun.org/">WooYun</a></li>
</ul>
<div>PS:</div>
<div>
<ul>
<li>今天又发现天涯4000万的用户数据，又是明文存储。不过事件的发展越来越阴谋论了，互联网在这里真好玩。（update: 2011-12-25)</li>
</ul>
</div>
</div>
<p>未完待续···</p>
<hr/><small><a href="http://blog.webfuns.net/archives/1259.html#comments" title="评论">评论</a>| 作者: <a href="http://blog.webfuns.net" >tomheng</a> 85159e08d5f1f50d244825cb03e92c65</small>]]></content:encoded>
			<wfw:commentRss>http://blog.webfuns.net/archives/1259.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL中LIKE的另类玩法</title>
		<link>http://blog.webfuns.net/archives/1254.html</link>
		<comments>http://blog.webfuns.net/archives/1254.html#comments</comments>
		<pubDate>Wed, 21 Dec 2011 11:48:27 +0000</pubDate>
		<dc:creator>tomheng</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.webfuns.net/?p=1254</guid>
		<description><![CDATA[设想一个场景，我有个关键词屏蔽词库（应该很多网站都有吧），所有的关键词都存储在MySQL中，怎样查询某个字符串是否含有屏蔽关键词？ 比如我们有一个屏蔽词 tom，要屏蔽任何含有tom的词语。 通常情况下LIKE这样用 SELECT * FROM `table` WHERE field LIKE &#8217;%字符串%&#8217; 下面我们这样玩，来解决上面的问题 SELECT * FROM `table` WHERE &#8216;字符串&#8217; LIKE CONCAT(&#8216;%&#8217;,field,&#8217;%') PS: 这让我想起了初中时数学课中经常用来做几何证明的逆向思考方式。 评论&#124; 作者: tomheng 85159e08d5f1f50d244825cb03e92c65]]></description>
			<content:encoded><![CDATA[<p>设想一个场景，我有个关键词屏蔽词库（应该很多网站都有吧），所有的关键词都存储在MySQL中，怎样查询某个字符串是否含有屏蔽关键词？</p>
<p>比如我们有一个屏蔽词 tom，要屏蔽任何含有tom的词语。</p>
<p>通常情况下LIKE这样用</p>
<p><span style="color: #0000ff;">SELECT * FROM `table` WHERE field LIKE &#8217;%字符串%&#8217;</span></p>
<p>下面我们这样玩，来解决上面的问题</p>
<p><span style="color: #0000ff;">SELECT * FROM `table` WHERE &#8216;字符串&#8217; LIKE CONCAT(&#8216;%&#8217;,field,&#8217;%')</span></p>
<p>PS:</p>
<p>这让我想起了初中时数学课中经常用来做几何证明的逆向思考方式。</p>
<hr/><small><a href="http://blog.webfuns.net/archives/1254.html#comments" title="评论">评论</a>| 作者: <a href="http://blog.webfuns.net" >tomheng</a> 85159e08d5f1f50d244825cb03e92c65</small>]]></content:encoded>
			<wfw:commentRss>http://blog.webfuns.net/archives/1254.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>seajs 使js开发模块化</title>
		<link>http://blog.webfuns.net/archives/1246.html</link>
		<comments>http://blog.webfuns.net/archives/1246.html#comments</comments>
		<pubDate>Sun, 11 Dec 2011 15:33:31 +0000</pubDate>
		<dc:creator>tomheng</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[seajs]]></category>

		<guid isPermaLink="false">http://blog.webfuns.net/?p=1246</guid>
		<description><![CDATA[seajs一个js类库，主要实现对js的模块化开发。 对于一个大项目或者是一个需要团队协作的项目，模块化开发 可以使得对代码的管理更规范和高效。seajs就是为了满足这种需求来的。 不过我个人可能会更需要js可以按需加载，因为有些类库不是所有的页面都 需要的，我的预期是js类库只有在需要的时候才去加载。 seajs可以通过require.async来实现按需加载。 最近在关注HTML5、CSS3的相关内容，这两个东西确实会给我们带来很不一样的体验。 还有js也很活跃，后端出了nodejs，前段有各种基础类库，还有MVC框架。 还有在js上发展起来的新语言 coffee script和dart,令人目不暇接。 确实很多东西(以论坛为主的网站将如何转型？)需要从底层变革一下了，因为他们周围的东西已经变了。 矛盾促使了这样的变革和发展。 更多内容：seajs.com 评论&#124; 作者: tomheng 85159e08d5f1f50d244825cb03e92c65]]></description>
			<content:encoded><![CDATA[<p><a href="seajs.com">seajs</a>一个js类库，主要实现对js的模块化开发。</p>
<p>对于一个大项目或者是一个需要团队协作的项目，模块化开发</p>
<p>可以使得对代码的管理更规范和高效。seajs就是为了满足这种需求来的。</p>
<p>不过我个人可能会更需要js可以按需加载，因为有些类库不是所有的页面都</p>
<p>需要的，我的预期是js类库只有在需要的时候才去加载。</p>
<p>seajs可以通过require.async来实现按需加载。</p>
<p>最近在关注HTML5、CSS3的相关内容，这两个东西确实会给我们带来很不一样的体验。</p>
<p>还有js也很活跃，后端出了<a href="http://nodejs.org/">nodejs</a>，前段有各种基础类库，还有MVC框架。</p>
<p>还有在js上发展起来的新语言<a href="http://jashkenas.github.com/coffee-script/"> coffee script</a>和<a href="http://www.dartlang.org/ ">dart</a>,令人目不暇接。</p>
<p>确实很多东西(<a href="http://www.zhihu.com/question/19938648">以论坛为主的网站将如何转型？</a>)需要从底层变革一下了，因为他们周围的东西已经变了。</p>
<p>矛盾促使了这样的变革和发展。</p>
<p>更多内容：seajs.com</p>
<hr/><small><a href="http://blog.webfuns.net/archives/1246.html#comments" title="评论">评论</a>| 作者: <a href="http://blog.webfuns.net" >tomheng</a> 85159e08d5f1f50d244825cb03e92c65</small>]]></content:encoded>
			<wfw:commentRss>http://blog.webfuns.net/archives/1246.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在什么场景下使用闭包？</title>
		<link>http://blog.webfuns.net/archives/1238.html</link>
		<comments>http://blog.webfuns.net/archives/1238.html#comments</comments>
		<pubDate>Wed, 07 Dec 2011 06:38:17 +0000</pubDate>
		<dc:creator>tomheng</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[闭包]]></category>

		<guid isPermaLink="false">http://blog.webfuns.net/?p=1238</guid>
		<description><![CDATA[在需要把逻辑封装到自己的范围内的情况下，闭包会十分有用。重构旧代码以进行简化并提高可读性就是这样一个例子。查看以下示例，该示例显示了在运行一些 SQL 查询时使用的记录程序。 12345678910//记录 SQL 查询的代码&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; $db = mysqli_connect&#40;&#34;server&#34;,&#34;user&#34;,&#34;pass&#34;&#41;; Logger::log&#40;'debug','database','Connected to database'&#41;; $db-&#62;query&#40;'insert into parts (part, description) values ('Hammer','Pounds nails'); Logger::log('debug','database','Insert Hammer into to parts table'); $db-&#62;query('insert into parts &#40;part, description&#41; values &#160; &#160; &#160; &#40;'Drill','Puts holes in wood'&#41;; Logger::log&#40;'debug','database','Insert Drill into to parts table'&#41;; $db-&#62;query&#40;'insert into parts [...]]]></description>
			<content:encoded><![CDATA[<p>在需要把逻辑封装到自己的范围内的情况下，闭包会十分有用。重构旧代码以进行简化并提高可读性就是这样一个例子。查看以下示例，该示例显示了在运行一些 SQL 查询时使用的记录程序。</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//记录 SQL 查询的代码&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><br />
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysqli_connect"><span style="color: #990000;">mysqli_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;server&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
Logger<span style="color: #339933;">::</span><a href="http://www.php.net/log"><span style="color: #990000;">log</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'debug'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'database'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Connected to database'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'insert into parts (part, description) values ('</span>Hammer<span style="color: #0000ff;">','</span>Pounds nails<span style="color: #0000ff;">'); <br />
Logger::log('</span>debug<span style="color: #0000ff;">','</span>database<span style="color: #0000ff;">','</span>Insert Hammer into to parts table<span style="color: #0000ff;">'); <br />
$db-&gt;query('</span>insert into parts <span style="color: #009900;">&#40;</span>part<span style="color: #339933;">,</span> description<span style="color: #009900;">&#41;</span> values <br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Drill'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Puts holes in wood'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
Logger<span style="color: #339933;">::</span><a href="http://www.php.net/log"><span style="color: #990000;">log</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'debug'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'database'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Insert Drill into to parts table'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'insert into parts (part, description) values ('</span>Saw<span style="color: #0000ff;">','</span>Cuts wood<span style="color: #0000ff;">'); <br />
Logger::log('</span>debug<span style="color: #0000ff;">','</span>database<span style="color: #0000ff;">','</span>Insert Saw into to parts table<span style="color: #0000ff;">');</span></div></td></tr></tbody></table></div>
<p>可以看出执行操作的重复程度。对 Logger::log() 执行的每次调用都有相同的前两个实参。为了解决此问题，我们可以把该方法调用放入闭包并转而针对该闭包执行调用。得到的代码如下所示：</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//记录 SQL 查询的重构代码&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><br />
<span style="color: #000088;">$logdb</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> Logger<span style="color: #339933;">::</span><a href="http://www.php.net/log"><span style="color: #990000;">log</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'debug'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'database'</span><span style="color: #339933;">,</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysqli_connect"><span style="color: #990000;">mysqli_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;server&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<span style="color: #000088;">$logdb</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Connected to database'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'insert into parts (part, description) values ('</span>Hammer<span style="color: #0000ff;">','</span>Pounds nails<span style="color: #0000ff;">'); <br />
$logdb('</span>Insert Hammer into to parts table<span style="color: #0000ff;">'); <br />
$db-&gt;query('</span>insert into parts <span style="color: #009900;">&#40;</span>part<span style="color: #339933;">,</span> description<span style="color: #009900;">&#41;</span> values <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Drill'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Puts holes in wood'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$logdb</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Insert Drill into to parts table'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'insert into parts (part, description) values ('</span>Saw<span style="color: #0000ff;">','</span>Cuts wood<span style="color: #0000ff;">'); <br />
$logdb('</span>Insert Saw into to parts table<span style="color: #0000ff;">');</span></div></td></tr></tbody></table></div>
<p>整理自：http://www.ibm.com/developerworks/cn/opensource/os-php-5.3new2/<br />
PS:原文有些不正确的地方，比方说到PHP 5.3.5都不可以通过闭包中的this指针访问类内的成员变量（当然可以通过其他方法实现），文章大部分内容正确，还是“尽信书不如无书”。</p>
<hr/><small><a href="http://blog.webfuns.net/archives/1238.html#comments" title="评论">评论</a>| 作者: <a href="http://blog.webfuns.net" >tomheng</a> 85159e08d5f1f50d244825cb03e92c65</small>]]></content:encoded>
			<wfw:commentRss>http://blog.webfuns.net/archives/1238.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>无主题分享一张图片</title>
		<link>http://blog.webfuns.net/archives/1223.html</link>
		<comments>http://blog.webfuns.net/archives/1223.html#comments</comments>
		<pubDate>Wed, 30 Nov 2011 15:28:08 +0000</pubDate>
		<dc:creator>tomheng</dc:creator>
				<category><![CDATA[web-trend]]></category>

		<guid isPermaLink="false">http://blog.webfuns.net/?p=1223</guid>
		<description><![CDATA[评论&#124; 作者: tomheng 85159e08d5f1f50d244825cb03e92c65]]></description>
			<content:encoded><![CDATA[<div id="attachment_1224" class="wp-caption aligncenter" style="width: 450px"><img class="size-full wp-image-1224" title="venn-diagram" src="http://blog.webfuns.net/wp-content/uploads/2011/11/venn-diagram.png" alt="" width="440" height="431" /><p class="wp-caption-text">猜猜中间哪个是什么？</p></div>
<p style="text-align: center;">
<hr/><small><a href="http://blog.webfuns.net/archives/1223.html#comments" title="评论">评论</a>| 作者: <a href="http://blog.webfuns.net" >tomheng</a> 85159e08d5f1f50d244825cb03e92c65</small>]]></content:encoded>
			<wfw:commentRss>http://blog.webfuns.net/archives/1223.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>【转载】浅谈PHP中的Session机制</title>
		<link>http://blog.webfuns.net/archives/1218.html</link>
		<comments>http://blog.webfuns.net/archives/1218.html#comments</comments>
		<pubDate>Mon, 28 Nov 2011 11:16:34 +0000</pubDate>
		<dc:creator>tomheng</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SESSION]]></category>

		<guid isPermaLink="false">http://blog.webfuns.net/?p=1218</guid>
		<description><![CDATA[做web开发，必然会涉及到Session，这是由于http协议本身是无状态的（每次响应都是独立的，彼此间没有联系），所以如果需要在页面跳转间保持某个用户的身份，就要在每次连接时告诉服务器端你的唯一标示号，即Session ID。这样，服务器端便可通过Session ID得到所需的数据。 在PHP中，Session是通过$_SESSION这个全局变量来set/get的，不过在使用之前要先初始化。初始化是通过session_start函数（如果php.ini中将session.auto_start设为1，则会自动初始化），之后PHP会为request自动生成一个唯一随机数作为Session ID，生成算法默认提供了MD5 (128 bits) 和SHA-1 (160 bits)，由php.ini中session.hash_function设定。其实也可以自定义，比如在随机数基础上将来访者的IP地址也加入到算法中，像CodeIgniter1.7.2中代码： 12345678$sessid = ''; while &#40;strlen&#40;$sessid&#41; &#60; 32&#41; &#123; &#160; &#160; $sessid .= mt_rand&#40;0, mt_getrandmax&#40;&#41;&#41;; &#125; // To make the session ID even more secure we'll combine it with the user's IP $sessid .= $this-&#62;CI-&#62;input-&#62;ip_address&#40;&#41;; $sessid = md5&#40;uniqid&#40;$sessid, TRUE&#41;&#41; 生成的ID存放在服务器的某一目录下，这由php.ini中session.save_path配置。如果你选择默认的文件式session存储，那么可能会遇到大量session文件导致IO性能下降，这个问题可以通过调节save_path来优化，具体请看大量php session临时文件带来的服务器效率问题。如果要在多个服务器中同步session id，你可以将其存放在数据库或共享缓存中。这需要你自定义一系列Session的读写方法，并在调用session_start函数前先设定好，以下面代码为例(来自php document中的一段示例代码)： 123456CREATE TABLE `ws_sessions` [...]]]></description>
			<content:encoded><![CDATA[<p>做web开发，必然会涉及到Session，这是由于http协议本身是无状态的（每次响应都是独立的，彼此间没有联系），所以如果需要在页面跳转间保持某个用户的身份，就要在每次连接时告诉服务器端你的唯一标示号，即Session ID。这样，服务器端便可通过Session ID得到所需的数据。</p>
<p>在PHP中，Session是通过$_SESSION这个全局变量来set/get的，不过在使用之前要先初始化。初始化是通过session_start函数（如果php.ini中将session.auto_start设为1，则会自动初始化），之后PHP会为request自动生成一个唯一随机数作为Session ID，生成算法默认提供了MD5 (128 bits) 和SHA-1 (160 bits)，由php.ini中session.hash_function设定。其实也可以自定义，比如在随机数基础上将来访者的IP地址也加入到算法中，像CodeIgniter1.7.2中代码：</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$sessid</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/strlen"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sessid</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">32</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$sessid</span> <span style="color: #339933;">.=</span> <a href="http://www.php.net/mt_rand"><span style="color: #990000;">mt_rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <a href="http://www.php.net/mt_getrandmax"><span style="color: #990000;">mt_getrandmax</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #666666; font-style: italic;">// To make the session ID even more secure we'll combine it with the user's IP</span><br />
<span style="color: #000088;">$sessid</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">input</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ip_address</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$sessid</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/md5"><span style="color: #990000;">md5</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/uniqid"><span style="color: #990000;">uniqid</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sessid</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>生成的ID存放在服务器的某一目录下，这由php.ini中session.save_path配置。如果你选择默认的文件式session存储，那么可能会遇到大量session文件导致IO性能下降，这个问题可以通过调节save_path来优化，具体请看<a href="http://apps.hi.baidu.com/share/detail/27379034">大量php session临时文件带来的服务器效率问题</a>。如果要在多个服务器中同步session id，你可以将其存放在数据库或共享缓存中。这需要你自定义一系列Session的读写方法，并在调用session_start函数前先设定好，以下面代码为例(来自php document中的一段示例代码)：</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`ws_sessions`</span> <span style="color: #66cc66;">&#40;</span> <br />
&nbsp; <span style="color: #ff0000;">`session_id`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">BINARY</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">,</span> <br />
&nbsp; <span style="color: #ff0000;">`session_expires`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; <span style="color: #ff0000;">`session_data`</span> text<span style="color: #66cc66;">,</span> <br />
&nbsp; <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`session_id`</span><span style="color: #66cc66;">&#41;</span> <br />
<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">TYPE</span><span style="color: #66cc66;">=</span>InnoDB;</div></td></tr></tbody></table></div>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <br />
<span style="color: #000000; font-weight: bold;">class</span> session <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// session-lifetime </span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$lifeTime</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// mysql-handle </span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$dbHandle</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">function</span> open<span style="color: #009900;">&#40;</span><span style="color: #000088;">$savePath</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sessName</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// get session-lifetime </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lifeTime</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/get_cfg_var"><span style="color: #990000;">get_cfg_var</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;session.gc_maxlifetime&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// open database-connection </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$dbHandle</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><a href="http://www.php.net/mysql_connect"><span style="color: #990000;">mysql_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;server&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$dbSel</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><a href="http://www.php.net/mysql_select_db"><span style="color: #990000;">mysql_select_db</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;database&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$dbHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// return success </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$dbHandle</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000088;">$dbSel</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbHandle</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbHandle</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">function</span> close<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">gc</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/ini_get"><span style="color: #990000;">ini_get</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'session.gc_maxlifetime'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// close database-connection </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #339933;">@</span><a href="http://www.php.net/mysql_close"><span style="color: #990000;">mysql_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">function</span> read<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sessID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// fetch session-data </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT session_data AS d FROM ws_sessions <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHERE session_id = '<span style="color: #006699; font-weight: bold;">$sessID</span>' <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AND session_expires &gt; &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// return data or an empty string at failure </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'d'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">function</span> write<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sessID</span><span style="color: #339933;">,</span><span style="color: #000088;">$sessData</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// new session-expire-time </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$newExp</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lifeTime</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// is a session with this id in the database? </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM ws_sessions <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHERE session_id = '<span style="color: #006699; font-weight: bold;">$sessID</span>'&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// if yes, </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// ...update session-data </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE ws_sessions <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SET session_expires = '<span style="color: #006699; font-weight: bold;">$newExp</span>', <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;session_data = '<span style="color: #006699; font-weight: bold;">$sessData</span>' <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHERE session_id = '<span style="color: #006699; font-weight: bold;">$sessID</span>'&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// if something happened, return true </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_affected_rows"><span style="color: #990000;">mysql_affected_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbHandle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// if no session-data was found, </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// create a new row </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO ws_sessions ( <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;session_id, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;session_expires, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;session_data) <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VALUES( <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'<span style="color: #006699; font-weight: bold;">$sessID</span>', <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'<span style="color: #006699; font-weight: bold;">$newExp</span>', <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'<span style="color: #006699; font-weight: bold;">$sessData</span>')&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// if row was created, return true </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_affected_rows"><span style="color: #990000;">mysql_affected_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbHandle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// an unknown error occured </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">function</span> destroy<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sessID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// delete session-data </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DELETE FROM ws_sessions WHERE session_id = '<span style="color: #006699; font-weight: bold;">$sessID</span>'&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// if session was deleted, return true, </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_affected_rows"><span style="color: #990000;">mysql_affected_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbHandle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// ...else return false </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">function</span> gc<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sessMaxLifeTime</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// delete old sessions </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DELETE FROM ws_sessions WHERE session_expires &lt; &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// return affected rows </span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <a href="http://www.php.net/mysql_affected_rows"><span style="color: #990000;">mysql_affected_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <br />
<span style="color: #009900;">&#125;</span> <br />
<span style="color: #000088;">$session</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> session<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<a href="http://www.php.net/session_set_save_handler"><span style="color: #990000;">session_set_save_handler</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$session</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;open&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$session</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;close&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$session</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;read&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$session</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;write&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$session</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;destroy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$session</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;gc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<a href="http://www.php.net/session_start"><span style="color: #990000;">session_start</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<span style="color: #666666; font-style: italic;">// etc... </span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>除了上述方法外还有其他办法可以保持Session的同步，可以参考<a href="http://www.fuchaoqun.com/2009/02/php-session-confuse/">PHP SESSION解惑</a>一文中第四部分“session的同步”。</p>
<p>下面再谈谈Session ID的传递方式：Cookie和URL传递。<br />
Cookie是比较常用的方式，在这种模式下，启动Session后服务器会在HTTP Response中自动加上header(‘Set-Cookie: session_name()=session_id(); path=/’)，并在以后的请求中加上这个Cookie。当从该页跳转到的新页面并调用session_start()后，PHP将检查与给定ID相关联的服务器端存贮的session数据，如果没找到，则新建一个数据集。但是有一点，这种传递方式必须在用户浏览器开启Cookie的情况下才可用，如果万一用户关闭了Cookie，那么只好选择另外一种通过URL参数传递Session ID。<br />
开启URL传递需要在php.ini中设置session.use_trans_sid（文档中提示使用这种方式会有安全风险，因为它显示地将Session ID放在url中，所以除非迫不得已不要选择此方式），并在代码中做如下修改：</p>
<div class="codecolorer-container php blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// 如果客户端使用cookie,可直接传递session到page2.php</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;br /&gt;&lt;a href=&quot;page2.php&quot;&gt;page 2&lt;/a&gt;'</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
<span style="color: #666666; font-style: italic;">// 如果客户端禁用cookie</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;br /&gt;&lt;a href=&quot;page2.php?'</span> <span style="color: #339933;">.</span> SID <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;page 2&lt;/a&gt;'</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>/*<br />
 默认php5.2.1下,SID只有在cookie被写入的同时才会有值,如果该session<br />
 对应的cookie已经存在,那么SID将为(未定义)空<br />
 */<br />
更新2010－12－28：<br />
如果php使用默认的file方式存储session，还要注意lock问题。因为php会lock住session文件直到这个session关闭，所以如果你的应用中涉及iframe、下载、Comet或者用户在同一个浏览器打开多个tab等等多个并行请求都要操作session时，就可能会遇到由于lock影响用户操作的情况。一个简单的解决办法就是在操作完session时，及时调用session_commit()或session_write_close()来关闭session，从而释放锁。（注意在关闭session后不要再调用任何session相关的函数）</p>
<p>关于Session的内容还有很多，具体可查看<a href="http://www.php.net/manual/en/book.session.php">官方手册</a>，如果有新的总结会继续更新。<br />
原文链接：http://imdonkey.com/blog/archives/255</p>
<hr/><small><a href="http://blog.webfuns.net/archives/1218.html#comments" title="评论">评论</a>| 作者: <a href="http://blog.webfuns.net" >tomheng</a> 85159e08d5f1f50d244825cb03e92c65</small>]]></content:encoded>
			<wfw:commentRss>http://blog.webfuns.net/archives/1218.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

