Php session advanced

Source: Internet
Author: User
Tags glob php session

Php session advanced

This section describes the advanced session usage.

Advanced session configuration in the php. ini configuration file:

Session. save_path: session storage address (original path: C:/Users/ADMINI ~ 1/AppData/Local/Temp)
Session_name: Name obtained by the session
Session. use_trans_sid: enable SID support

Session. gc_maxlifetime: specifies the number of seconds after which the data will be treated as garbage and cleared.

Session. gc_probability = 1
Session. gc_divisor = 100
These two combinations are the probability of starting gc process management.
During initialization (session_start ())
Probability = session. gc_probability/session. gc_divisor

Session. use_cookie = 1: whether to use cookie
Session. cookie_path = '/': cookie storage path
Session. cookie_domain = '': you do not need to change it.
Session. cookie_lifetime = 0: Long Storage Time

Session. save_handler = files: Use a file to save the session Letter and change it to user.
Session. save_handler = memcache: If it is memcache, then save_path = tcp: // localhost: 11211

 

1. Change the session. save_handler value to user (originally files) in the configuration file, and change the session data storage path to disk D.

 

2. Create related files in the root directory

Session. php is the public header file, one. php is the session registration Event file, two. php is the test file, and three. php is the logout file.

One. php:

 

 ";

 

 

Two. php:
 ';    echo session_name().'='.session_id()."";

Three. php:
 ";

It is relatively important for the common header file session. php. The value of session. serialize_handler = file must be changed to user.

Session. php should use the session_set_save_handler () function, view the parameters used by the manual, and then enable session

 

session_set_save_handler("open","close","read","write","destroy","gc");session_start();

Write the methods based on the parameters, and focus on the execution time of each function.

 

Open ():

 

 
// Run session_start () to call the function open ($ save_path, $ session_name) {global $ sess_save_path; // set the Save path to the global variable $ sess_save_path = $ save_path; // return true to the Save path ;}

 

 

Close ():
// Session_wirte_close () and session_destroy () function close () {return true ;}

 

Read ():
// Session_start (), $ _ SESSION // automatically transmits session_id to function read ($ id) {global $ sess_save_path; $ sess_file = $ sess_save_path. "/kf _". $ id; return (string) @ file_get_contents ($ sess_file );}

Write ():
// When the script ends and SESSION data is forcibly submitted using session_write_close (), // call $ _ SESSION [] = "aaa"; function write ($ id, $ sess_data) {global $ sess_save_path; // defines the file to be saved and the file name $ sess_file = $ sess_save_path. "/kf _". $ id; if ($ fp = @ fopen ($ sess_file, "w") {$ return = fwrite ($ fp, $ sess_data); fclose ($ fp ); return $ return;} else {return false ;}}

Story () and gc (): (delete and garbage collection)

 

// When session_destroy (), function destroy ($ id) {global $ sess_save_path; $ sess_file = $ sess_save_path. "/kf _". $ id; // delete the file return @ unlink ($ sess_file) of the path object;} // session. gc_probability and session. gc_divisor determines that when reading data through open () and read (), function gc ($ maxlifetime) {global $ sess_save_path; // delete all expired objects // glob traverses all objects with "/kf" as the foreach (glob ($ sess_save_path. "/kf _ *") as $ filename) {if (filetime ($ filename) + $ maxlifetime <time () {@ unlink ($ filename ); echo $ filename ;}} return true ;}




 

 

 

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.