Home > php教程 > php手册 > 快速入门:PHP Session用法

快速入门:PHP Session用法

WBOY
Release: 2016-06-13 11:05:20
Original
898 people have browsed it

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>
Copy after login

同 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>
Copy after login

当我们执行 setage.php 文件的时候,调用了 setage() 方法,设置了年龄为 21,并将该状态序列化后保存在 Session 中(PHP 将自动完成这一转换),当转到 output.php 后,要输出这个值,就必须反序列化刚才保存的对象,又因为在解序列化的时候需要实例化一个未定义类,所以我们定义了以后回调函数,自动包含 person.php 这个类文件,因此对象被重构,并取得当前 age 的值为 21,然后调用 output() 方法输出该值。另外,我们还可以使用 session_set_save_handler 函数来自定义 PHP Session用法。
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template