Home > Backend Development > PHP Tutorial > Quick Start: PHP Session Usage_PHP Tutorial

Quick Start: PHP Session Usage_PHP Tutorial

WBOY
Release: 2016-07-15 13:28:23
Original
1213 people have browsed it

After a long period of development of PHP, many users know PHP very well. Here I will express my personal understanding and discuss with you the usage of PHP Session. For virtual hosts, if all user sessions are saved in the system temporary folder, it will cause difficulty in maintenance and reduce security. We can manually set the save path of the Session file. session_save_path() provides such a Function. We can point the Session storage directory to a folder that cannot be accessed through the Web. Of course, the folder must have read-write attributes.

PHP Session usage:

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

Same as the session_set_cookie_params(); function, the session_save_path() function must also be called before the session_start() function is called. We can also store arrays and objects in Session. There is no difference between operating an array and operating a general variable. When saving an object, PHP will automatically serialize the object (also called serialization) and then save it in the Session. The following example illustrates this:

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

When we execute the setup.php file, the setup is called () method, set the age to 21, and serialize the status and save it in the Session (PHP will automatically complete this conversion). When going to output.php, to output this value, you must deserialize just now The saved object needs to instantiate an undefined class during deserialization, so we define a later callback function that automatically includes the person.php class file, so the object is reconstructed and the current age value is obtained. 21, and then call the output() method to output the value. In addition, we can also use the session_set_save_handler function to customize PHP Session usage.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446464.htmlTechArticleAfter a long period of development of PHP, many users know PHP very well. Here I will express my personal understanding and share it with you. Discuss PHP Session usage. For virtual hosts, if all users use...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template