QuickStart: PHP Session Usage _php Tutorial

Source: Internet
Author: User
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:

 
  
  
  1. !--? php
  2. //Set a directory
  3. $ savepath = ;
  4. //save one day
  5. $ LifeTime = 24 * 3600;
  6. Session_save_path ($savePath);
  7. session_set_cookie_params ($lifeTime);
  8. session_start ();
  9. $_session["admin"] = true;
  10. "

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

 
 
  1. Php
  2. Class Person
  3. {
  4. var $age;
  5. function output () {
  6. Echo $this- > Age ;
  7. }
  8. function Setage ($age) {
  9. $this- > Age = $age;
  10. }
  11. }
  12. ?>
  13. setage.php
  14. Php
  15. Session_Start ();
  16. Require_once "person.php";
  17. $ Person = New Person ();
  18. $person- > Setage (21);
  19. $_session[' person '] = $person;
  20. echo " <a href=' output '>check here for output age a> ";
  21. ?>
  22. output.php
  23. Set the callback function to ensure that the object is rebuilt.
  24. Ini_set (' Unserialize_callback_func ', ' mycallback ');
  25. function Mycallback ($classname) {
  26. Include_once $classname. ". PHP";
  27. }
  28. Session_Start ();
  29. $ Person = $_session["Person"];
  30. Output 21
  31. $person- > output ();
  32. ?>

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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.