Session is a server-side technology. With this technology, the server can create an exclusive session file for each user's browser at runtime. Because session is exclusive to the user's browser, therefore, when users access the Web Resources of the server, they can put their own data in their respective sessions. When the user accesses other web resources on the server again (the session is not terminated ), other web resources then retrieve data from their respective sessions to serve users.
Session is the most important user behavior tracking technology (preventing illegal user login/Verification Code, etc ).
By default, session files are stored in the C:/Windows/TEMP directory.
The following is the sessionCrudOperations
1. Create a session
<? PHP // Create a session // Initialize session Session_start (); // Save data $ _ Session [ ' Name ' ] = " Pawm " ; // In the session file, dobule, integer, String, bool, array, and object can be saved. // Save bool integer $ _ Session [ ' Age ' ] = 20 ; $ _ Session [ ' Isboy ' ] = True ; $ _ Session [ ' Arr ' ] = Array ( " Beijing " , " Zhang San " , " Xiaoming " ); // Save the object to the session Class Dog { Private $ Name; Private $ Age; Private $ Intro; Public Function _ construct ($ name, $ age, $ intro) {$ This -> Name = $ Name; $ This -> Age = $ Age; $ This -> Intro = $ Intro ;}$ dog1 = New Dog ( " Dog " , 5 , " Good dog " ); $ _ Session [ ' Dog1 ' ] =$ Dog1; echo " Saved " ; ?>
Key points:
- 1. Saved Data Format: Name | S: 4: "pawm"; age | I: 20; isboy | B: 1;
Name: is the key
S: indicates the data type.
4: indicates the data size.
- 2. The data types that can be saved in the session are dobule, integer, String, bool, array, and object.
2. Retrieve the session
<? PHP require_once " Dog. Class. php " ; Echo " <PRE> " ; // The session must be initialized when used. Session_start (); // Retrieve all Print_r ($ _ session); echo " </PRE> " ; // Single fetch // $ Name = $ _ session ['name']; If (! Empty ($ _ session [ ' Name ' ]) {Echo " The name is: " . $ _ Session [ ' Name ' ];} Else {Echo ' No name <br> ' ;} If (! Empty ($ _ session [ ' Age ' ]) {Echo " Age: " . $ _ Session [ ' Age ' ]." <Br> " ;;} Else {Echo ' Age is gone <br> ' ;} // Echo $ _ session ['isboys']; // Output 1 If (! Empty ($ _ session [ ' Arr ' ]) {$ Arr = $ _ Session [ ' Arr ' ]; Foreach ($ Arr As $ Key => $ Val) {echo " $ Val " ;}} Else {Echo ' No more arr <br> ' ;} Echo " <Br> " ; // When retrieving an instance of an object, you must introduce the class information. Otherwise, an error occurs. If (! Empty ($ _ session [ ' Dog1 ' ]) {$ Dog = $ _ Session [ ' Dog1 ' ]; // Var_dump ($ dog ); Echo " <Br> " . $ Dog-> Show (); // Echo $ dog-> name; // Echo 'name is '. $ name. "Age is". $ age; } Else {Echo ' Dog Missing <br> ' ;} ?>
3. Update the session
<?PHP//Update sessionSession_start ();//Update name$ _ Session ['Name'] ="James"; Echo"Updated";?>
4. delete a session
<?PHP//Delete sessionSession_start ();//Single Delete, delete name//Unset ($ _ session ['name']);//Delete allSession_destroy (); echo"Session deleted";?>