Quick Start: PHP Session usage

Source: Internet
Author: User

PHP has been developing for a long time and many users are familiar with PHP. Here I will share my personal understanding and discuss with you the usage of PHP Session. 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:

 
  1. <?Php
  2. // Set a storage directory
  3. $SavePath="./Session_save_dir /";
  4. // Save for 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. ?>

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

 
  1. <?Php
  2. Class person
  3. {
  4. Var $ age;
  5. Function output (){
  6. Echo $ this->Age;
  7. }
  8.  
  9. Function setAge ($ age ){
  10. $ This->Age= $ Age;
  11. }
  12. }
  13. ?>
  14. Setage. php
  15. <?Php
  16. Session_start ();
  17. Require_once "person. php ";
  18. $Person=NewPerson ();
  19. $ Person->SetAge (21 );
  20. $ _ SESSION ['person '] = $ person;
  21. Echo"<A Href='Output'>Check here to output age</A>";
  22. ?>
  23. Output. php
  24. <?
  25. // Set the callback function to re-build the object.
  26. Ini_set ('unserialize _ callback_func ', 'mycallback ');
  27. Function mycallback ($ classname ){
  28. Include_once $ classname. ". php ";
  29. }
  30. Session_start ();
  31. $Person= $ _ SESSION ["person"];
  32. // Output 21
  33. $ Person->Output ();
  34.  
  35. ?> 

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.


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.