Customize session storage in PHP

Source: Internet
Author: User
Tags apc

Description

In PHP, you can define session_set_save_handler to store Server session data on different media, such as files, apc or memcache caches, or databases. You can calculate the number of online users, or kick the login status of specific members.

The custom session_set_save_handler basically overwrites the system's default session read/write method to manage sessions. You are welcome to join us. Please write to me if you have any questions.


Factory

[Php]
<? Php
 
/**
* CHttpSession
* Http session data storage engine
*/
 
Class CHttpSession {
 
Private static $ engine;
Private static $ gc_maxlifetime;
 
Public static function engine ($ enginer ){
$ Enginer = ucfirst ($ enginer );
$ EngineInstance = "CHttpSession {$ enginer }";
$ Filename = SYS_MODULE. '/Session/'. $ engineInstance. '. php ';
 
If (! File_exists ($ filename )){
Throw new Exception ('fatal: not found {$ filename} file ');
}
Require ($ filename );
 
If (! Class_exists ($ engineInstance )){
Throw new Exception ('fatal: not found {$ engineInstance} object ');
}
 
$ Handler = new CHttpSession (new $ engineInstance );
 
Ini_set ("session. save_handler", "user ");
Ini_set ('apc. ttl ', 3600 );
Ini_set ('apc. user_ttl ', 1200 );
Ini_set ('apc. gc_ttl ', 3600 );
 
Session_set_save_handler (
Array ($ handler, 'open '),
Array ($ handler, 'close '),
Array ($ handler, 'read '),
Array ($ handler, 'write '),
Array ($ handler, 'deststroy '),
Array ($ handler, 'gc ')
);

If (isset ($ _ COOKIE ['phpsessid ']) {
Session_start ($ _ COOKIE ['phpsessid ']);
}
Else {
Session_start ();
Setcookie ('phpsessid ', session_id (), null,'/', COOKIE_DOMAIN );
}
}
 
Public function _ construct (& $ engine ){
Self: $ engine = $ engine;
Self: $ gc_maxlifetime = ini_get ('session. gc_maxlifetime ');
}
 
Public function read ($ id ){
Return self: $ engine-> fetch ('session/'. $ id );
}
 
Public function write ($ id, $ data ){
Return self: $ engine-> add ('session/'. $ id, $ data, self: $ gc_maxlifetime );
}
 
Public function close (){
Return true;
}
 
 
Public function destroy ($ id ){
Return self: $ engine-> delete ('session/'. $ id );
}
 
Public function _ destruct (){
Session_write_close ();
}
 
 
Public function gc ($ maxlifetime ){
Return true;
}
 
 
Public function open ($ save_path, $ session_name ){
Return true;
}
};

Method

CHttpSessionFile

[Php]
<? Php
 
/**
* CFileHttpSession
* The session engine stores sessions as files. The YPi framework defaults to the session storage engine.
* SESSION_DIR: Set the session file storage path
*/
 
 
Class CHttpSessionFile {
 
Public function add ($ key, $ data, $ cg_maxlifetime ){
$ Filepath = substr ($ key, 7 );
File_put_contents (SESSION_DIR. $ filepath, $ data );
Return true;
}
 
Public function fetch ($ key ){
$ Filepath = substr ($ key, 7 );
If (! File_exists (SESSION_DIR. $ filepath )){
File_put_contents (SESSION_DIR. $ filepath ,'');
Return true;
}
Return file_get_contents (SESSION_DIR. $ filepath );
}
 
Public function delete ($ key ){
$ Filepath = substr ($ key, 7 );
If (file_exists (SESSION_DIR. $ filepath )){
Unlink (SESSION_DIR. $ filepath );
}
Return true;
}
};


CHttpSessionApc

[Php]
<? Php
 
/**
* CApcHttpSession
* The session engine stores sessions in APC cache mode.
* Set SESSION_ENGINE to apc to store sessions in APC mode.
*/
 
 
Class CHttpSessionApc {
 
Public function add ($ key, $ data, $ cg_maxlifetime ){
Apc_store ($ key, $ data, $ cg_maxlifetime );
Return true;
}
 
Public function fetch ($ key ){
If (! Apc_exists ($ key )){
Apc_store ($ key ,'');
Return true;
}
Return apc_fetch ($ key );
}
 
Public function delete ($ key ){
If (apc_exists ($ key )){
Apc_delete ($ key );
}
Return true;
}
};

CHttpSessionMemcache

[Php]
<? Php
 
/**
* CMemcacheHttpSession
* The session engine stores sessions in memcache cache mode *
* Set SESSION_ENGINE to memcache to store sessions in memcache mode.
* Set the memcache server address for MEMCACHE_HOST
* MEMCACHE_PORT: Set the access port number of the memcache server.
*/
 
 
Class CHttpSessionMemcache {
 
Private static $ memcache;
 
Public function _ constrct ($ config ){
Self: $ memcache = new Memcache;
Self: $ memcache-> connect (MEMCACHE_HOST, MEMCACHE_PORT );
}
 
Public function _ destroy (){
Self: $ memcache-> close ();
}
 
Public function add ($ key, $ data, $ cg_maxlifetime ){
Return self: $ memcache-> add ($ key, $ data, $ cg_maxlifetime );
}
 
Public function fetch ($ key ){
Return self: $ memcache-> get ($ key );
}
 
Public function delete ($ key ){
Return self: $ memcache-> delete ($ key );
}
};


Instance description

You only need to replace session_start () with the following method.

[Php]
<? Php
 
Defined ('session _ ENGINE ') | define ('session _ ENGINE', 'file ');
Require '../lib/CHttpSession. php ';
 
CHttpSession: engine (SESSION_ENGINE );

 


From yagas's column

Related Article

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.