The method of PHP implementing session custom conversation processor _php Tips

Source: Internet
Author: User
Tags sessions

This article illustrates the method of PHP implementing session customization. Share to everyone for your reference. The specific analysis is as follows:

Session-defined conversation processor, that is to say, all the operations on the session can be decided by the custom sessions. What does that mean? First look at the Session.save_handler configuration in php.ini.

By default, Session.save_handler = files, which means that the system-defined processor is invoked (the so-called processor, in fact, is a lot of functions/methods). You can set the Session.save_handler to user or memcache, or even a network file system (cloud computing).

Session.save_handler = User: Represents a call to a custom session processor; Session.save_handler = Memcache: Indicates ... Slightly.... (Mostly stored in memory, more efficient).

When setting Session.save_handler = files, the action on the session actually calls the six callback functions of the Session.set_save_handler (specifically looking at the PHP manual) (the so-called callback function, which is the system call, You do not need to specify a call). The six callback functions are shown in the following code, which is also available in the PHP manual:

Copy Code code as follows:
<?php

function open ($save _path, $session _name)
{
Global $sess _save_path;
$sess _save_path = $save _path;
return (true);
}

function Close ()
{
return (true);
}

function Read ($id)
{
Global $sess _save_path;
$sess _file = "$sess _save_path/sess_$id";
Return (String) @file_get_contents ($sess _file);
}

function Write ($id, $sess _data)
{
Global $sess _save_path;
$sess _file = "$sess _save_path/sess_$id";
if ($fp = @fopen ($sess _file, "W")) {
$return = fwrite ($fp, $sess _data);
Fclose ($FP);
return $return;
} else {
return (false);
}
}

function Destroy ($ID)
{
Global $sess _save_path;
$sess _file = "$sess _save_path/sess_$id";
Return (@unlink ($sess _file));
}

Function GC ($MAXLIFETIME)
{
Global $sess _save_path;
foreach (Glob ("$sess _save_path/sess_*") as $filename) {
if (Filemtime ($filename) + $maxlifetime < time ()) {
@unlink ($filename);
}
}
return true;
}

Session_set_save_handler ("Open", "close", "read", "write", "Destroy", "GC");
Session_Start ();
Proceed to use sessions normally
?>


When you set the Session.save_handler to user, you can rewrite the code above and change it to the way you want it to be called in the Execute PHP file. For example, by default, we save the session file name, which starts with the Sess_, which is actually set here, and you can change it to what you want.

Of course, you can also modify the session save the way, by default is saved in the file, you can save to the database (of course, do not recommend that you do this, the database read more slowly), can also be set to save in memory (the fastest, in the Memcache related content has detailed explanation).

I hope this article will help you with your PHP program design.

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.