PHP经过长时间的发展,很多用户都很了解PHP了,这里我发表一下个人理解,和大家讨论讨论PHP Session用法。对于虚拟主机来说,如果所有用户的Session都保存在系统临时文件夹里,将给维护造成困难,而且降低了安全性,我们可以手动设置 Session 文件的保存路径,session_save_path() 就提供了这样一个功能。我们可以将 Session 存放目录指向一个不能通过 Web 方式访问的文件夹,当然,该文件夹必须具备可读写属性。
PHP Session用法:
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>// 设置一个存放目录 </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>savePath</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>"./session_save_dir/"</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>// 保存一天 </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>lifeTime</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>24</FONT></SPAN><SPAN> * 3600; </SPAN></SPAN><LI class=""><SPAN>session_save_path($savePath); </SPAN><LI class=alt><SPAN>session_set_cookie_params($lifeTime); </SPAN><LI class=""><SPAN>session_start(); </SPAN><LI class=alt><SPAN>$_SESSION["admin"] = true; </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
同 session_set_cookie_params(); 函数一样,session_save_path() 函数也必须在 session_start() 函数调用之前调用。我们还可以将数组,对象存储在 Session 中。操作数组和操作一般变量没有什么区别,而保存对象的话,PHP 会自动对对象进行序列化(也叫串行化),然后保存于 Session 中。下面例子说明了这一点:
person.php
<ol class="dp-xml"> <li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>class person </SPAN><LI class=alt><SPAN>{ </SPAN><LI class=""><SPAN>var $age; </SPAN><LI class=alt><SPAN>function output() { </SPAN><LI class=""><SPAN>echo $this-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span>age; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>function setAge($age) { </span></li> <li class=""> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">age</font></span><span> = $age; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class=""><span>setage.php </span></li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>session_start(); </SPAN><LI class=alt><SPAN>require_once "person.php"; </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>person</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>new</FONT></SPAN><SPAN> person(); </SPAN></SPAN><LI class=alt><SPAN>$person-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>setAge(21); </span> </li> <li class=""><span>$_SESSION['person'] = $person; </span></li> <li class="alt"> <span>echo "</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>a</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>href</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>'output'</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>check here to output age</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>a</SPAN><SPAN class=tag>></span></font></strong><span>"; </span> </li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class="alt"><span>output.php </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"><?</FONT></STRONG></SPAN><SPAN> </SPAN></SPAN><LI class=alt><SPAN>// 设置回调函数,确保重新构建对象。 </SPAN><LI class=""><SPAN>ini_set('unserialize_callback_func', 'mycallback'); </SPAN><LI class=alt><SPAN>function mycallback($classname) { </SPAN><LI class=""><SPAN>include_once $classname . ".php"; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN>session_start(); </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>person</FONT></SPAN><SPAN> = $_SESSION["person"]; </SPAN></SPAN><LI class=""><SPAN>// 输出 21 </SPAN><LI class=alt><SPAN>$person-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span>output(); </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>