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"