This article mainly introduces the SESSION class implemented by php, including session creation, initialization, and reading? How many seconds does it mean? It is a very practical technique. if you need it, you can refer to the example in this article to describe the SESSION class implemented by php. Share it with you for your reference. The specific analysis is as follows:
SESSION applications in php are essential and one of the most important functions. A session is called a "SESSION" in a network application. it is generally understood to store the information required for a specific user SESSION. in this way, when a user redirects between website pages, the stored SESSION value will not be lost, but will survive throughout the user SESSION. Generally speaking, when user A accesses the internet, it creates an ID (a) value to save it. if your ID (A) value is not canceled, this website will still remember your ID (A) value. at this time, you can call your ID (A) value online. for example, you are welcome to ID () value.
It is very easy to apply the SESSION value in PHP. you only need to input session_start () at the top to start the SESSION. now you can use the SESSION. this is only an application method for small websites, in fact, the SESSION itself has many attributes, such as SESSION cycle, SESSION call, SESSION data validity period, SESSION storage, and SESSION cancellation. if these attributes are available, it seems to be a relatively standard SESSION application SESSION.
The following is a complete Session class that integrates the most basic attribute values of a Session. opening, closing, and cleaning are in line with php programming specifications, which is also a good habit. For a small note, if the website does not use a large number of Session classes, it is basically unnecessary to use the SESSION class.
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
{
/**
* Default session validity period
* @ Access public
* @ Var ineger $ _ expiry
*/
Public $ _ expiry = 3600;
/**
* Valid domain name
* @ Access public
* @ Var string $ _ domain
*/
Public $ _ domain = '.bitsCN.com ';
// 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, 'deststroy '),
Array (& $ this, 'gc ')
);
Session_start ();
}
/**
* Open a 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 the 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 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;
}
/**
* Destroy a 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;
}
/**
* Clear 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 session ');
Return true;
}
I hope this article will help you with PHP programming.