Quick start: PHPSession usage _ PHP Tutorial

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

 
 
  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. 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=NewPerson ();
  18. $ Person->SetAge (21 );
  19. $ _ SESSION ['person '] = $ person;
  20. Echo"<A Href='Output'>Check here to output age A>";
  21. ?>
  22. Output. php
  23. // Set the callback function to re-build the object.
  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 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...

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.