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>
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>