SESSION class implemented by php, SESSION class implemented by php_PHP tutorial

WBOY
Release: 2016-07-13 10:12:40
Original
732 people have browsed it

php implements the SESSION class, php implements the SESSION class

The example in this article describes the SESSION class implemented by PHP. Share it with everyone for your reference. The specific analysis is as follows:

The application of SESSION in php is essential and one of the most important functions. SESSION is called "session" in network applications. We usually understand it as storing the information required for a specific user session. In this way, when the user jumps between website pages, the stored SESSION value will not be lost, but will be used throughout the session. Survives throughout the user session. In layman's terms, when user A goes online, an ID (a) value will be created and saved. If your ID (A) value is not logged out, the website will remember your ID (A) the next time you go online. ) value, at this time you can call your ID (A) value online. For example, you are welcome to visit your ID (A) value again.

It is very simple to apply the SESSION value in PHP. Just enter session_start() at the top to start the session. Then you can use SESSION. This is just an application method for small websites. In fact, SESSION itself is also There are many attributes, such as SESSION cycle, calling SESSION, SESSION data validity period, SESSION save, SESSION logout, etc. If you have these attributes, it seems to be a relatively standardized SESSION application session.

The following is a complete Session class, which integrates the most basic attribute values ​​​​of Session. Among them, opening, closing and cleaning are in line with PHP programming specifications, which is also a good habit. A small note, if the website does not use the Session class extensively, there is basically no need to use the SESSION class.

Copy code The code is as follows:
/**
* 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';
//初始化
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();
}
/**
* 打开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;
}
/**
* 关闭session
* @access public
* @return bool
*/
public function close()
{
return mysql_close($this->_conn);
}
/**
* 读取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 serialized session content
* @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;
}
/**
* 销毁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 up expired sessions
* @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;
}

希望本文所述对大家的PHP程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/920608.htmlTechArticlephp实现的SESSION类,php实现SESSION类 本文实例讲述了php实现的SESSION类。分享给大家供大家参考。具体分析如下: 关于 SESSION 在 php 中的应用是...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template