<div class="title_bot"><script>ec(2);</script></div> <br><br><span style="COLOR: #000000"></span><span style="COLOR: #000000">php <br></span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">*<br> * session处理器 <br> * memcache 实现<br> * @author MoXie SysTem128@GMail.Com <br> </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> SessionHandler </span><span style="COLOR: #0000ff">extends</span><span style="COLOR: #000000"> Memcache<br>{<br> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$_sessionId</span><span style="COLOR: #000000">; </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000"> session 编号</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$_sessionExpire</span><span style="COLOR: #000000">; </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000"> session 过期时间</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$_sessionIsQuery</span><span style="COLOR: #000000">;<br> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000"> __construct()<br> {<br> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">_sessionExpire </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">600</span><span style="COLOR: #000000">;<br> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">connect(</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">localhost</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">11211</span><span style="COLOR: #000000">);<br> }<br> </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">*<br> * 获取所有定位字段 <br> * <br> </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br><br> </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">*<br> * 读取 Session <br> * <br> * @return unknown <br> </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000"> getSession(</span><span style="COLOR: #800080">$snKey</span><span style="COLOR: #000000">)<br> {<br> </span><span style="COLOR: #800080">$returnInfo</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">get(</span><span style="COLOR: #800080">$snKey</span><span style="COLOR: #000000">);<br> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">_sessionIsQuery </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> (bool)</span><span style="COLOR: #800080">$returnInfo</span><span style="COLOR: #000000">;<br> </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$returnInfo</span><span style="COLOR: #000000">;<br> }<br> </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">*<br> * 插入新的Session <br> </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000"> insertSession(</span><span style="COLOR: #800080">$snKey</span><span style="COLOR: #000000">,</span><span style="COLOR: #800080">$value</span><span style="COLOR: #000000">)<br> {<br> </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">set(</span><span style="COLOR: #800080">$snKey</span><span style="COLOR: #000000">,</span><span style="COLOR: #800080">$value</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">_sessionExpire);<br> }<br> </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">*<br> * 更新session信息 <br> * <br> * @return unknown <br> </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000"> updateSession(</span><span style="COLOR: #800080">$snKey</span><span style="COLOR: #000000">,</span><span style="COLOR: #800080">$value</span><span style="COLOR: #000000">)<br> {<br> </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000"> 修改语句</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">set(</span><span style="COLOR: #800080">$snKey</span><span style="COLOR: #000000">,</span><span style="COLOR: #800080">$value</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">_sessionExpire);<br> }<br> </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">*<br> * 删除Session <br> * <br> * @return unknown <br> </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000"> delSession(</span><span style="COLOR: #800080">$snKey</span><span style="COLOR: #000000">)<br> {<br> </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">delete(</span><span style="COLOR: #800080">$snKey</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br> }<br> </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">*<br> * 删除过期 Session <br> * <br> * @return unknown <br> </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000"> expireSession()<br> {<br> </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br> }<br> </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">*<br> * session 启动器 <br> * <br> </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br><br> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000"> on_session_start()<br> {<br> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">_sessionSite </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">_sessionId </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #008080">session_id</span><span style="COLOR: #000000">();<br> </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br> }<br> </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">*<br> * session 终止 <br> </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000"> on_session_end()<br> {<br> </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">expireSession();<br> }<br> </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">*<br> * 读取方法 <br> * <br> * @param unknown_type $key <br> </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000"> on_session_read(</span><span style="COLOR: #800080">$key</span><span style="COLOR: #000000">)<br> {<br> </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">getSession(</span><span style="COLOR: #800080">$key</span><span style="COLOR: #000000">);<br> }<br> </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">*<br> * 写入方法 <br> * <br> * @param unknown_type $key <br> * @param unknown_type $value <br> </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000"> on_session_write(</span><span style="COLOR: #800080">$key</span><span style="COLOR: #000000">,</span><span style="COLOR: #800080">$value</span><span style="COLOR: #000000">)<br> {<br> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">getSession(</span><span style="COLOR: #800080">$key</span><span style="COLOR: #000000">);<br> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">_sessionIsQuery)<br> {<br> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">updateSession(</span><span style="COLOR: #800080">$key</span><span style="COLOR: #000000">,</span><span style="COLOR: #800080">$value</span><span style="COLOR: #000000">);<br> }</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">{<br> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">insertSession(</span><span style="COLOR: #800080">$key</span><span style="COLOR: #000000">,</span><span style="COLOR: #800080">$value</span><span style="COLOR: #000000">);<br> }<br> </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br> }<br> </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">*<br> * 销毁方法 <br> * <br> * @param unknown_type $key <br> </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000"> on_session_destroy(</span><span style="COLOR: #800080">$key</span><span style="COLOR: #000000">)<br> {<br> </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">delSession();<br> }<br> </span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">*<br> * 过期方法 <br> * <br> * @param integer $maxLifeTime <br> </span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"> <br> </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000"> on_session_gc(</span><span style="COLOR: #800080">$maxLifeTime</span><span style="COLOR: #000000">)<br> {<br> </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">$this</span><span style="COLOR: #000000">-></span><span style="COLOR: #000000">expireSession();<br> }<br>}<br><br></span><span style="COLOR: #800080">$sessionHandler</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> SessionHandler();<br></span><span style="COLOR: #008080">session_set_save_handler</span><span style="COLOR: #000000">(<br></span><span style="COLOR: #0000ff">array</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">&</span><span style="COLOR: #800080">$sessionHandler</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">on_session_start</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">array</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">&</span><span style="COLOR: #800080">$sessionHandler</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">on_session_end</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">array</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">&</span><span style="COLOR: #800080">$sessionHandler</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">on_session_read</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">array</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">&</span><span style="COLOR: #800080">$sessionHandler</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">on_session_write</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">array</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">&</span><span style="COLOR: #800080">$sessionHandler</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">on_session_destroy</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">array</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">&</span><span style="COLOR: #800080">$sessionHandler</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">on_session_gc</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">)<br>);<br></span><span style="COLOR: #008080">unregister_tick_function</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">session_write_close</span><span style="COLOR: #000000">''</span><span style="COLOR: #000000">);<br><br></span><span style="COLOR: #008080">session_start</span><span style="COLOR: #000000">();<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">$_SESSION[''MoXie''] = ''Wonderfull!'';<br>//$_SESSION[''SysTem128''] = ''Wonderfull!'';<br>//session_unregister(''MoXie'');</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">print_r</span><span style="COLOR: #000000">(</span><span style="COLOR: #800080">$_SESSION</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #000000">?></span><span style="COLOR: #000000"> </span> <br><br><p id="TBPingURL"></p>