Heim >Backend-Entwicklung >PHP-Tutorial >快速入门:PHP Session用法_PHP教程
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> <li class=""><span>// 设置一个存放目录 </span></li> <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> </li> <li class=""><span>// 保存一天 </span></li> <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> </li> <li class=""><span>session_save_path($savePath); </span></li> <li class="alt"><span>session_set_cookie_params($lifeTime); </span></li> <li class=""><span>session_start(); </span></li> <li class="alt"><span>$_SESSION["admin"] = true; </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></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> <li class=""><span>class person </span></li> <li class="alt"><span>{ </span></li> <li class=""><span>var $age; </span></li> <li class="alt"><span>function output() { </span></li> <li class=""> <span>echo $this-</span><span class="tag"><strong><font color="#006699">></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> </li> <li class=""><span>session_start(); </span></li> <li class="alt"><span>require_once "person.php"; </span></li> <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> </li> <li class="alt"> <span>$person-</span><span class="tag"><strong><font color="#006699">></font></strong></span><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 class="tag-name">a</span></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">></font></strong></span><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> </li> <li class="alt"><span>// 设置回调函数,确保重新构建对象。 </span></li> <li class=""><span>ini_set('unserialize_callback_func', 'mycallback'); </span></li> <li class="alt"><span>function mycallback($classname) { </span></li> <li class=""><span>include_once $classname . ".php"; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>session_start(); </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">person</font></span><span> = $_SESSION["person"]; </span> </li> <li class=""><span>// 输出 21 </span></li> <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>