PHP implementation of the session class _php skills

Source: Internet
Author: User
Tags php programming sessions sprintf

This example describes the session class for PHP implementation. Share to everyone for your reference. The specific analysis is as follows:

About the session in PHP application is necessarily not less, is one of the most important functions. Session in network applications, called "sessions", we usually understand to store the information required for a particular user session, so that when a user jumps between pages of a site, the stored session value is not lost, but is kept alive throughout the user conversation. In layman's terms, when user A is online, an ID (a) value is created to save the If your ID (a) value is not logged out, the next time you surf the web, the site will also remember your ID (a) value, this time can be used to call your ID (a) value, such as welcome your ID (a) Value is once again accessed.

About the application of the session in PHP is very simple, as long as the top input session_start () to start the conversation, the following can be used, this is just a small site application method, in fact, session itself there are many attributes, such as Session cycle, call Session,session data validity period, sessions save, and so on, and so on, if these attributes, it seems to be a comparison of the application of a standardized conversation.

The following is a complete session class that incorporates the most basic attribute values of the session, in which, open, close and clean is in line with the PHP programming specification, which is also a good habit. A small explanation, if the site is not a lot of use session class, basically there is no need to use the session class.

Copy Code code 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 Valid time
* @access Public
* @var Ineger $_expiry
*/
public $_expiry = 3600;
/**
* Valid domain name
* @access Public
* @var String $_domain
*/
Public $_domain = '. Jb51.net ';
Class
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 session content 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;
}
/**
* Clean 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 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.