Php implements session-defined session processor

Source: Internet
Author: User
This article describes how to implement the session custom session processor in php. you can set the session. save_handler is a user that calls a custom callback function to implement the session custom session processor function, which is very useful, for more information about how to implement a session custom session processor in php, see the following example. Share it with you for your reference. The specific analysis is as follows:

The session custom session processor, that is, all the operations on the session can be decided by the custom session. What does it mean? Let's take a look at the session. save_handler configuration in php. ini.

By default, session. save_handler = files indicates that the processor defined by the system is called (the so-called processor is actually a lot of functions/methods ). You can set session. save_handler to user or memcache, or even a network file system (cloud computing ).

Session. save_handler = user: indicates to call a custom session processor; session. save_handler = memcache: indicates... .... (It is generally stored in the memory, which is more efficient ).

When the session is set. when save_handler = files, session operations are actually called. set_save_handler (for details, refer to the php Manual) six callback functions (the so-called callback function is called by the system and does not need to be called ). See the following code for the six callback functions:

The code is 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 session. save_handler to user, you can rewrite the above code to the method you need and call it in the PHP file. For example, by default, the names of the session files that we save start with sess _, which is actually set here. you can change it to what you want.

Of course, you can also modify the session storage method, which is saved in a file by default, and you can save it to the database (of course you are not recommended to do so, the database reading speed is slower ), you can also save the settings in the memory (the fastest speed, which is detailed in memcache ).

I hope this article will help you with php programming.

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.