PHP custom Session Sample Share _php instance

Source: Internet
Author: User
Tags garbage collection sessions

The following is the code for the session class

Copy Code code as follows:

<?php
Class session
{
static function init ()
{
Session_set_save_handler (
Array ("Session", "open"),
Array ("session", "Close"),
Array ("session", "read"),
Array ("Session", "write"),
Array ("Session", "destroy"),
Array ("Session", "GC")
);
}

static function open ($save _path, $session _name)
{
echo "Session opening!<br>";
/*global $db, $REMOTE _addr;
$rs = $db->execute ("select * from Sessions where sessionid= '". session_id (). "'");
$arry = $rs->fetchrow ();
if ($rs && $arry)
{
$db->execute ("Update Sessions set Sessionlast=now () where sessionid= '". session_id (). "'");
}
Else
{
$query = "INSERT into Sessions set sessionid= '". session_id (). "', sessionname= ' $REMOTE _addr ', sessionlast= ' Now () '";
Echo $query;
$db->execute ($query);
}*/
return true;
}
static function Close ()
{
return (true);
}

static function read ($id)
{
echo "Session reading Now!<br>";
Global $db;
return true;
$timenow = strftime ("%y-%m-%d%h:%m:%s", Time ());
$query = "Select Sessiondata from Sessions where sessionid= ' $id ' and sessionlast > ' $timenow '";
$rs = $db->execute ($query);
if (list ($SessionData) = $rs->fetchrow ())
{
Echo $SessionData;
return $SessionData;
}
Else
{
return false;
}
}

static function Write ($id, $sess _data)
{
echo "Session writing now!<br>";
Global $db;
$rs = $db->execute ("Select SessionID from Sessions where sessionid= ' $id ')";
$num = $rs->recordcount ();
$unix _time = time () +my_sess_time;
Echo my_sess_time;
$dateleft = strftime ("%y-%m-%d%h:%m:%s", $unix _time);
if ($num <= 0)
{
$sql = "INSERT into Sessions set sessiondata= ' $sess _data ', Sessionname= '". $_server["REMOTE_ADDR"]. "', sessionlast= ' $ Dateleft ', sessionid= '. session_id (). "'";
}
Else
{
$sql = "Update Sessions set sessiondata= ' $sess _data ', Sessionname= '". $_server["REMOTE_ADDR"]. "', sessionlast= ' $ Dateleft ' where sessionid= ' $id ';
}
$db->execute ($sql);
}

static function Destroy ($ID)
{
echo "Session destroying Now!<br>";
Global $db;
$sql = "DELETE from Sessions WHERE ' SessionID ' = ' $id '";
$rs = $db->execute ($sql);
return $rs;
$sess _file = "$sess _save_path/sess_$id";
Return (@unlink ($sess _file));
}

/*********************************************
* Warning-you'll need to implement some *
* Sort of garbage collection routine here. *
*********************************************/
static function gc ($MAXLIFETIME)
{
echo "Session Maxlifetime now!<br>";
Global $db;
$timenow = strftime ("%y-%m-%d%h:%m:%s", Time ());
$sql = "DELETE from ' $table _sessions ' WHERE ' sessionlast ' < ' $timenow '";
Return $sess _db->execute ($sql);
echo "Now gc!<br>";
return true;
}
Proceed to use sessions normally
}

How to use

Copy Code code as follows:

Include ("session.class.php");
Session::init ();
Session_Start ();
Define ("My_sess_time", 3600); Session Survival Time Long
$_session["test"] = "abcdef";

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.