customizing session Storage in PHP _php tutorial

Source: Internet
Author: User
Describe

In PHP, you can define Session_set_save_handler to store server session data on different media, such as stored in a file, APC or memcache cache, or stored in a database. You can count the number of people online, or kick off a particular member's landing status, among other things.

Custom Session_set_save_handler basically uses a custom read-write method that overrides the system's default session read-write method to achieve the management of the session. Welcome to communicate with you, have any questions please write to me.


Factory class

[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, ' destroy '),
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;
}
};

Specific methods

Chttpsessionfile

[PHP]

/**
* Cfilehttpsession
* Session engine, the session is stored as a file, Ypi framework default session storage engine
* Session_dir set 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]

/**
* Capchttpsession
* Session engine, which stores the session in APC cache mode
* Session_engine set value to APC to enable APC to store SESSION
*/


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]

/**
* Cmemcachehttpsession
* Session engine to store session in Memcache cache *
* Session_engine Setting value is memcache to enable memcache way to store SESSION
* Memcache_host set MEMCACHE server address
* Memcache_port Set MEMCACHE server access port number
*/


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);
}
};


Example description

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

[PHP]

Defined (' Session_engine ') | | Define (' Session_engine ', ' file ');
Require '. /lib/chttpsession.php ';

Chttpsession::engine (Session_engine);


Excerpt from Yagas's column

http://www.bkjia.com/PHPjc/478213.html www.bkjia.com true http://www.bkjia.com/PHPjc/478213.html techarticle description in PHP can be defined by the Session_set_save_handler, the server session data stored on different media, such as stored in a file, APC or memcache cache, or stored in the number ...

  • 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.