Start a session? Register a session? Use a session? Delete a session
1. Start the session
Session_Start ()
Creating a session with the Session_register () function
The Session_register () function is used to login a variable for the session implicitly to start the session, but requires the option to php.ini the file, set the register_globals instruction to ON,
Then restart the Apache server.
Note: when calling Session_register (), you do not need to call the Session_Start () function, and PHP will implicitly call the Session_Start () function after registering the variable.
2. Registering a session
After the session is started, it is all saved in $_session. Creating a session variable from an array $_session is easy, as long as you add elements directly to the array.
3. Using sessions
First, determine if a session variable has a session ID present, create one without being present, and make it accessible through the global array $_session. If present, the session variable is loaded for use by the user.
For example, determine if the session variable is empty, not empty, and copy to $myvalue.
4. Delete a session
(1) Delete a single session
Use Unset (), but you cannot use the unset ($_session) function to destroy global variables $_session, cannot be restored, and users can no longer register $_session variables.
unset ( $_session [ ' user ' ]);
(2) Delete multiple sessions
$_session = array ();
(3) End session
Session_destroy ();
Session setup Time
1. The client does not prohibit cookies
(1) Session_set_cookie_params () must be called before Session_Start ()
Description: This function is not recommended and some browsers have problems.
(2) using Setcookie ()
2. The client prohibits cookies
(1) Open cookies before logging in, many forums do so
(2) Hide form pass through Get method session_id (Common)
(3) Use file or database storage session_id, manually invoke in Page pass
====================== =========== cut ============ line ================
Session Advanced Application
1.Session temp File
Session_save_path () stores session temp files to mitigate server inefficiencies and slow site opening due to temporary file storage
Example:
Note: Session_save_path () between the session_start () functions
2.Session Cache
Session cache is to store content in the IE client's temporary Internet Files folder, you can set the cache time, the next time to read the cache content, thereby speeding up.
Session Cache uses the Session_cache_limiter () function
String Session_cache_limiter ([ string Cache_limiter])
The parameter cache_limiter is public or private. At the same time the session cache is not on the server side but in the client cache, not displayed on the server.
Cache time, using the Session_cache_expire () function
int session_cache_expire ([int new_cache_expire])
Example:
3.session Database Storage
Session_set_save_handler () function
BOOL Session_set_save_handler ( string open , string close , string read , string write , string destroy , string GC)
Parameters |
Description |
Open (Save_path,session_name) |
Find session storage address, remove variables |
Close () |
Do not require parameters to close the database |
Read (key) |
Read session key value, key corresponds to session_id |
Write (Key,data) |
Where data corresponds to the setting of the session variable
|
Destroy (Key) |
Logoff session corresponding session key value |
GC (Expiry_time) |
Clear expired session Record |
Example:
$time "; $result = mysql_query ($sql, $handle), $row = Mysql_fetch_array ($result), if ($row) {return ($row [' Session_data ']) ;//return session name and content}else{return (FALSE);}} function _session_write ($key, $data) {global $handle; $time = 60*60;//set expiration Time $lapse_time = time () + $time;//Get UNIX Timestamp $sql = "Select Session_data from tb_session where Session_key = ' $key ' and session_time > $lapse _time"; $result = mysql_query ( $sql, $handle); if (mysql_num_rows ($result) = = 0)//no result {$sql = "insert into tb_session values (' $key ', ' $data ', $lapse _time ) ";//INSERT Database SQL statement $result = mysql_query ($sql, $handle);} else{$sql = "Update tb_session set session_key = ' $key ', session_data = ' $data ', session_time = $lapse _time where Session_ke y = ' $key ';//Modify Database SQL statement $result = mysql_query ($sql, $handle);} return ($result);} function _session_destroy ($key) {global $handle; $sql = "Delete from tb_session where Session_key = ' $key '";//delete database SQL statement " Esult = mysql_query ($sql, $handle); return ($result);} function _session_gc ($expiry _time) {global $handle; $lapsE_time = time ();//assigns the parameter $expiry_time to the current timestamp $sql = "Delete from tb_session where Expiry_time < $lapse _time";//delete database SQL statement $result = mysql_query ($sql, $handle); return ($result);} Session_set_save_handler (' _session_open ', ' _session_close ', ' _session_read ', ' _session_write ', ' _session_destroy ', ' _session_gc '); Session_Start (); $_session[' user '] = ' Mr '; $_session[' pwd '] = ' mrsoft ';? >
====================== =========== cut ============ line ================