PHP implementation of the session class, PHP implementation session Class _php tutorial

Source: Internet
Author: User

PHP implementation of the session class, PHP implementation session class


In this paper, we describe the session class of PHP implementation. Share to everyone for your reference. The specific analysis is as follows:

About the application of the SESSION in PHP is not less, is one of the most important features. Session in Web applications, called "Sessions", we generally understand the information required to store a specific user session, so that when users jump between site pages, the stored session value is not lost, but the entire user session has survived. In layman's words, when user A is online, an ID (a) value is created to save it, and if your ID (a) value is not logged off, the next time you surf the web, the site will remember your ID (a) value, and you can call your ID (a) on the Internet, such as Welcome ID (a) Value is accessed once again.

About the application of the session value in PHP is very simple, as long as the top input session_start () to start the session, the following can be used session, this is only a small site application method, in fact, the session itself has many properties, such as Session period, call Session,session data validity period, session save, session logoff and so on, if you have these attributes, it seems to be a relatively standardized session application sessions.

The following is a complete session class, the integration of the session's most basic property values, which, open, close and clean is in line with the PHP programming code, which is also a good habit. Small explanation, if the site is not a large number of use of the session class, basically there is no need to use the session class.
Copy CodeThe code is as follows: <?php
/**
* File Description Session Class
* =================================================================
* File name session.class.php
* -----------------------------------------------------------------
* Applicable environment: Php5.2.x/mysql 5.0.x
* -----------------------------------------------------------------
* Author 04ie. Com
* -----------------------------------------------------------------
* Creation Time 2010-2-1
* =================================================================
*/
Class Session
{
/**
* Session Default Validity time
* @access Public
* @var Ineger $_expiry
*/
public $_expiry = 3600;
/**
* Valid domain name
* @access Public
* @var String $_domain
*/
Public $_domain = '. Jb51.net ';
Initialization
Public Function __construct ()
{
Ini_set (' session.use_trans_id ', 0);
Ini_set (' Session.gc_maxlifetime ', $this->_expiry);
Ini_set (' Session.use_cookie ', 1);
Ini_set (' Session.cookie_path ', '/');
Ini_set (' Session.cookie_domain ', $this->_domain);
Session_module_name (' user ');
Session_set_save_handler (
Array (& $this, ' Open '),
Array (& $this, ' Close '),
Array (& $this, ' read '),
Array (& $this, ' write '),
Array (& $this, ' Destroy '),
Array (& $this, ' GC ')
);
Session_Start ();
}
/**
* Open Session
* @access Public
* @param string $savePath
* @param string $sName
* @return True
*/
Public function open ($savePath, $sName)
{
$this->_conn = mysql_connect (' localhost ', ' root ', ');
mysql_select_db (' databases ');
mysql_query (' SET NAMES ' utf8 "');
return true;
}
/**
* Close Session
* @access Public
* @return BOOL
*/
Public Function Close ()
{
Return Mysql_close ($this->_conn);
}
/**
* Read session
* @access Public
* @param string $sid SessionID
* @return Mixed
*/
Public function read ($SID)
{
$sql = "Select data from Sessions WHERE sessionid= '%s '";
$sql = sprintf ($sql, $sid);
$res = mysql_query ($sql, $this->_conn);
$row = Mysql_fetch_assoc ($res);
Return! $row? Null: $row [' data '];
}
/**
* Write session
* @access Public
* @param string $sid SessionID
* @param string $data Serialize the contents of the session after serialization
* @return
*/
Public function Write ($sid, $data)
{
$expiry = time () + $this->_expiry;
$sql = "REPLACE into sessions (Sessionid,expiratio
N,data) VALUES ('%s ', '%d ', '%s ');
$sql = sprintf ($sql, $sid, $expiry, $data);
mysql_query ($sql, $this->_conn);
return true;
}
/**
* Destroy session
* @access Public
* @param string $sid SessionID
* @return
*/
Public function Destroy ($SID)
{
$sql = "DELETE from sessions WHERE sessionid= '%s '";
$sql = sprintf ($sql, $sid);
mysql_query ($sql, $this->_conn);
return true;
}
/**
* Cleanup expired session
* @access Public
* @param integer $time
* @return
*/
Public Function gc ($time = 0)
{
$sql = "DELETE from sessions WHERE expiration < '%d '";
$sql = sprintf ($sql, Time ());
mysql_query ($sql, $this->_conn);
mysql_query (' OPTIMIZE TABLE sessions ');
return true;
}

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/920608.html www.bkjia.com true http://www.bkjia.com/PHPjc/920608.html techarticle PHP implementation of the session class, PHP implementation session class This article describes the PHP implementation of the session class. Share to everyone for your reference. The specific analysis is as follows: The application of the SESSION in PHP is ...

  • Related Article

    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.