Quick start: PHPSession usage. After a long history of PHP development, many users are familiar with PHP. here I will share my personal understanding and discuss PHPSession usage with you. For virtual hosts, if PHP has been used for a long period of development, many users are familiar with PHP. here I will share my understanding and discuss the PHP Session usage with you. For a VM, if all users' sessions are saved in a temporary system folder, maintenance is difficult and security is reduced. you can manually set the Session file storage path, session_save_path () provides such a function. We can direct the Session directory to a folder that cannot be accessed through the Web. of course, this folder must have the read/write attribute.
PHP Session usage:
- Php
- // Set a storage directory
- $SavePath="./Session_save_dir /";
- // Save for one day
- $LifeTime=24* 3600;
- Session_save_path ($ savePath );
- Session_set_cookie_params ($ lifeTime );
- Session_start ();
- $ _ SESSION ["admin"] = true;
- ?>
Like 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 sessions. There is no difference between an operation array and an operation variable. if you save an object, PHP will automatically serialize the object (also called serialization) and save it in the Session. The following example illustrates this:
Person. php
- Php
- Class person
- {
- Var $ age;
- Function output (){
- Echo $ this->Age;
- }
-
- Function setAge ($ age ){
- $ This->Age= $ Age;
- }
- }
- ?>
- Setage. php
- Php
- Session_start ();
- Require_once "person. php ";
- $Person=NewPerson ();
- $ Person->SetAge (21 );
- $ _ SESSION ['person '] = $ person;
- Echo"<A Href='Output'>Check here to output age A>";
- ?>
- Output. php
-
- // Set the callback function to re-build the object.
- Ini_set ('unserialize _ callback_func ', 'mycallback ');
- Function mycallback ($ classname ){
- Include_once $ classname. ". php ";
- }
- Session_start ();
- $Person= $ _ SESSION ["person"];
- // Output 21
- $ Person->Output ();
-
- ?>
When we execute setage. in the php file, the setage () method is called, the age is set to 21, and the status is serialized and saved in the Session (PHP will automatically complete this conversion ), when it is switched to output. after php, to output this value, you must deserialize the saved object. because an undefined class needs to be instantiated during deserialization, we have defined the callback function in the future, automatically contains person. php class file. Therefore, the object is restructured and the current age value is 21. then, the output () method is called to output the value. In addition, we can use the session_set_save_handler function to customize PHP Session usage.
Reset Session usage. For a VM...