Why am I not using session?

Source: Internet
Author: User

When considering the session, I finally gave up the session:
1. The original session is managed using files. The quality of the file system directly affects the performance of the session, especially when several K people are online at the same time. There are two solutions: database and file (using the hash path ).
2. the scalability and controllability of the original session are poor. It is not conducive to combining my existing system.
3. When I used a database, I didn't select sqlite. The efficiency I tested last time was not as good as I thought in win xp.

Finally, I chose the myql heap table to process the session, and combined the session processing with online user statistics.

The code below is one of my instances (no other details are provided, and the specific usage needs to be modified accordingly)


<?
/**
* Process online users and simulate sessions
* Create table 'webqq _ session '(

'Sid 'char (32) not null,
'Uid' mediumint (8) not null,
'Username' char (80) not null,
'Ismember' tinyint (1) not null,
'Logintime' int (10) not null,
'Activetime' int (10) not null,
Primary key ('sid ')
) ENGINE = heap default charset = gb2312
* @ Author: feifengxlq * @ Since: 2006-10-23
* @ Copyright: http://www.phpobject.net
* Note: The use of this file must be combined with other files: for example, filtering cookid, some basic functions and Database Operations
*/
Class session
{
Var $ mysql;

Var $ cookie_id = webqq_sid;

Var $ session = array ();

Var $ max_time = 1200; // The default maximum time is 20 minutes.

Function _ construct ()
{
$ This-> mysql = new module (TB_PREX. _ session); // external support required
$ This-> start ();
}

Function start ()
{
If (empty ($ _ COOKIE [$ this-> cookie_id])
{
// Initialize the session
$ This-> create ();
} Else {
// Check whether the cookie already exists in the Database
$ Sid = $ _ COOKIE [$ this-> cookie_id];
If ($ this-> mysql-> detail (where sid =. $ sid .))
{
// The database exists.
$ Row [activetime] = time ();
$ This-> mysql-> update ($ row, where sid =. $ sid .);
$ This-> session = $ this-> mysql-> detail (where sid =. $ sid .);
} Else {
// The database does not exist.
$ This-> create ();
}
}
// Delete offline users in the database
$ This-> mysql-> delete (where activetime +. $ this-> max_time. <. time ());
}

Function register ($ name, $ value, $ update = false)
{
If (array_key_exists ($ name, $ this-> session )){
$ This-> session [$ name] = $ value;
}
If ($ update) $ this-> update ();
}

Function registry ($ name =)
{
If (empty ($ name) return $ this-> session;
If (array_key_exists ($ name, $ this-> session )){
Return $ this-> session [$ name];
}
}
// Update session information in the database
Function update ()
{
$ Row [uid] = $ this-> session [uid];
$ Row [username] = $ this-> session [username];
$ Row [ismember] = $ this-> session [ismember];
$ Row [logintime] = $ this-> session [logintime];
$ Row [activetime] = $ this-> session [activetime];
$ This-> mysql-> update ($ row, where sid =. $ this-> session [sid].);
}
/* ------------------- The following is the private method ------------------------------------------------------**/
Function create ()
{
$ Nowtime = time ();
$ Sid = md5 (0. $ nowtime. getip (); // You must define the getip () function in advance to obtain the customer's IP address.
Setcookie ($ this-> cookie_id, $ sid, $ nowtime + 3600*24); // The default value is 24 hours.
$ Row [sid] = $ sid;
$ Row [uid] = 0;
$ Row [username] = guest;
$ Row [ismember] = 0;
$ Row [logintime] = $ nowtime;
$ Row [activetime] = $ nowtime;
$ This-> mysql-> add ($ row); // write data to the database
$ This-> session = $ row;
}
}
?>

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.