PHP after a long period of development, many users are very familiar with PHP, here I publish a personal understanding, and we discuss the use of PHP session. For the virtual host, if all the user's Session is saved in the System temporary folder, will make maintenance difficult, and reduce security, we can manually set the Session file save path, Session_save_path () provides such a function. We can point the Session directory to a folder that cannot be accessed by the Web, and of course the folder must have read/write properties.
PHP Session Usage:
- !--? php
- //Set a directory
- $ savepath = ;
- //save one day
- $ LifeTime = 24 * 3600;
- Session_save_path ($savePath);
- session_set_cookie_params ($lifeTime);
- session_start ();
- $_session["admin"] = true;
- "
With Session_set_cookie_params (); function, the Session_save_path () function must also be called before the session_start () function call. We can also store arrays, objects in the Session. There is no difference between manipulating an array and manipulating a general variable, and if you save the object, PHP automatically serializes the object (also called serialization) and then saves it in the Session. This is illustrated in the following example:
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 = New Person ();
- $person- > Setage (21);
- $_session[' person '] = $person;
- echo " <a href=' output '>check here for output age a> ";
- ?>
- output.php
-
- Set the callback function to ensure that the object is rebuilt.
- 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 the setage.php file, we call the Setage () method, set the age to 21, and then serialize the state to the Session (PHP will automatically complete the conversion), when you go to output.php, to output this value, You have to deserialize the object you just saved, and because you need to instantiate an undefined class at the time of the deserialization, we define a later callback function that automatically contains the Person.php class file, so the object is refactored, takes the current age value to 21, and then calls output () method to output the value. In addition, we can use the Session_set_save_handler function to customize the PHP session usage.
http://www.bkjia.com/phpjc/446464.html Span id= "Indexurl" itemprop= "Indexurl" >www.bkjia.com true http://www.bkjia.com/phpjc/446464.html techarticle php after a long period of development, A lot of users are very familiar with PHP, here I publish a personal understanding, and we discuss the use of PHP session. For a virtual host, if all ...