PHP Session usage, PHPSession usage

Source: Internet
Author: User

PHP Session usage, PHPSession usage

1)InitializationSession

Session_start ();

2)DirectionSessionAdd data

$ _ SESSION ["key"] = $ val

3)SlaveSessionObtain a value

$ Val = $ _ SESSION [key]

4)DeleteSessionAssociated data

Delete specified data:Unset ($ _ SESSION [Key]);

Delete all data:Session_destory ();

 

1 <? Php 2 // start session 3 session_start (); 4 // **************** add session data 5 // $ _ SESSION ['city'] = "Shanghai "; 6 $ _ SESSION ['id'] = 5; 7 $ _ SESSION ['bool '] = true; 8 // array 9 $ arr = array ("Beijing ", "2", "China"); 10 $ _ SESSION ['array'] = $ arr; 11 // object 12 class Cat {13 public $ name; 14 public $ age; 15 public $ intro; 16 17 function _ construct ($ name, $ age, $ intro) {18 $ this-> name = $ name; 19 $ this-> age = $ age; 20 $ this-> intro = $ intro; 21} 22} 23 $ cat = New Cat ("kitten", "2", "White"); 24 $ _ SESSION ['cat'] = $ cat; 25 26 // ************ get session27 // 28 // echo $ _ SESSION ['city']; 29 var_dump ($ _ SESSION ['array']); 30 var_dump ($ _ SESSION ['array'] [0]); 31 32 // var_dump ($ _ SESSION ['cat']); // object 33 34 // ************* update session data 35 // $ _ SESSION ['city'] = "Beijing "; 36 // $ _ SESSION ['array'] [0] = "Shenzhen "; 37 38 // ************** Delete session39 // unset ($ _ SESSION ['name']); 40 // unset ($ _ SESSION ['array'] [1]); 41 // unset ($ _ SESSION ['cat']); // delete all objects, this will delete the session file 44 session_destroy (); 45 46 var_dump ($ _ SESSION); 47 echo "<br/> success"; 48?>

Summary

1)To useSession, All must be initializedSession_start ();

2)SessionYou can add multipleKey => valNote:KeyIt cannot be repeated,ValIt can be a basic data type and array/Object

3)If we want to retrieve the object, we need to declare the definition information of the class.

SessionDetails

SessionThe default data storage time is1400Second (24Minutes), this time can passPhp. iniFile Modification,Session. gc_maxlifetime = 1440,The time can be customized. You need to restart the instance after modification.Apache,This time is specified in1440Not used in secondsSessionFile,SessionWill be recycled as a junk file

SessionThe file storage path can be modified.Php. iniFile,Session. save_path = "c:/mysession"

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.