Session refers to the time that a user spends browsing a website from entering the website to closing the browser. From the above definition, we can see that Session is actually a specific concept of time.
When using PHP to apply a session, the session data is stored on the server, and the client information is identified by the sessionID sent from the client, and information is extracted.
Your own instance
The Code is as follows: |
Copy code |
<? Php Session_start (); // start the session, which must be started in the session $ _ SESSION ['name'] = "fangshan"; // sets the session, and the value of name is fangshan. ?> <A href = "session2.php"> next </a> (Save as session. php) <? Php Session_start (); // start the session If (isset ($ _ SESSION ['name']) // you can determine whether a SESSION exists. { Echo $ _ SESSION ['name']; Echo "<a href =" deletesession. php "> del session </a> "; } Else { Echo "Session has not been setted "; } ?> (Save As session2.php) <? Php Session_start (); // start the session Unset ($ _ SESSION ['name']); // deregister the session variable Session_destroy (); // clear the session ID Header ("Location: session2.php"); // jump back to the front page ?> (Save As deletesession. php) |
Common session operations:
Write, read, register, and delete sessions.
Start of session
The START function of the session is session_start, and session_start is used to initialize the session variable. Syntax:
Session_start ();
Returns TRUE.
Write and read sessions
In PHP, sessions are called and read through the predefined array $ _ session.
On the website page, assign values to the $ _ SESSION array on the registration page, and read the $ _ SESSION array on other pages.
Register the session on the page, for example:
The Code is as follows: |
Copy code |
<? Php Session_start (); $ _ SESSION ['keyword'] = "php "; ?>
|
Sessions on other pages, such:
The Code is as follows: |
Copy code |
<? Php Session_start (); Echo $ _ SESSION ['keyword']; ?>
|
Run the command in sequence and the result is:
Php
Session detection and Cancellation
Do you still remember isset and unset functions? These two functions implement session detection and cancellation respectively.
The isst function is used to check whether a session already exists. The syntax is as follows:
Bool isset ($ _ SESSION ['session _ name'])
For example:
The Code is as follows: |
Copy code |
<? Php Session_start (); If (isset ($ _ SESSION ['keyword']) Echo $ _ SESSION ['keyword']; Else Echo "www. bKjia. c0m "; ?> |
Result:
Php
The usset function is used to deregister a created session variable. Syntax:
Unset ($ _ SESSION ['session _ name'])
For example:
The Code is as follows: |
Copy code |
<? Php Session_start (); Unset ($ _ SESSION ['keyword']); If (isset ($ _ SESSION ['keyword']) Echo $ _ SESSION ['keyword']; Else Echo "www. bKjia. c0m "; ?> |
Result:
Www. bKjia. c0m
Php processes a total of 10 Session functions. We will introduce several functions in detail.
1. session_start
Function: Start a session or return an existing session.
Function prototype: boolean session_start (void );
Return Value: Boolean
Function Description: This function has no parameters and returns true. It is best to place this function first, and there cannot be any output before it, otherwise an alarm will be triggered, such as: Warning: cannot send session cache limiter-headers already sent (output started at/usr/local/apache/htdocs/cga/member/1.php: 2) in/usr/local/apache/htdocs/cga/member/1.php on line 3
2. session_register
Function: registers a new variable as a session variable.
Function prototype: boolean session_register (string name );
Return Value: Boolean value.
Function Description: This function adds a variable to the current SESSION in the global variable. The parameter name is the name of the variable to be added. If it succeeds, the logical value true is returned. You can use $ _ SESSION [name] or $ HTTP_SESSION_VARS [name] To set values or assign values.
3. session_is_registered
Function: checks whether a variable is registered as a session variable.
Function prototype: boobean session_is_registered (string name );
Return Value: Boolean
Function Description: This function checks whether a specified variable has been registered in the current session. The parameter name is the name of the variable to be checked. If the call succeeds, the logical value true is returned.
4. session_unregister
Function: delete a registered variable.
Function prototype: boolean session_session_unregister (string name );
Return Value: Boolean
Function Description: This function deletes the variables in the global variables in the current session. The parameter name is the name of the variable to be deleted. If it is successful, true is returned.
5. Session_destroy
Function: ends the current session and clears all resources in the session.
Function prototype: boolean session destroy (void );
Return Value: Boolean value.
Function Description: This function ends the current session. This function has no parameters and returns true.
The functions described above will be used in the following sections, but there are also some functions related to the session:
6. session_encode
Function: sesssion Information encoding
Function prototype: string session_encode (void );
Return Value: String
Function Description: The returned string contains the names and values of each variable in the global variable, in the form of a | s: 12: "it is a test"; c | s: 4: "lala"; a is the variable name s: 12 represents the value of variable a "it is a test the length is 12 variables are separated by semicolons.
7. session_decode
Function: decodes sesssion information.
Function prototype: boolean session_decode (string data)
Return Value: Boolean
Function Description: This function decodes session information. If the session information is successful, the logical value true is returned.
8. session_name
Function: access the current session name.
Function prototype: boolean session_name (string [name]);
Return Value: String
Function Description: This function can obtain or reset the name of the current session. If the parameter name is not set, the current session name is obtained. If the parameter is added, the session name is set to the parameter name.
9. session_id
Function: access the ID of the current session.
Function prototype: boolean session_id (string [id]);
Return Value: String
Function Description: This function can obtain or reset the ID number of the currently stored session. If no parameter id is specified, only the id of the current session is obtained. If a parameter is added, the id of the session is set to the new id.
10. session_unset
Function: delete all registered variables.
Function prototype: void session_unset (void)
Return Value: Boolean
Function Description: Unlike Session_destroy, this function does not end a session. Just like using the session_unregister function to cancel all session variables one by one.