Php-implemented SESSION class, php-implemented SESSION class _ PHP Tutorial

Source: Internet
Author: User
Php implements the SESSION class and php implements the SESSION class. Php-implemented SESSION class. php-implemented SESSION class this article describes the SESSION class implemented by php. Share it with you for your reference. The specific analysis is as follows: the application of SESSION in php is the SESSION class implemented by php, and the SESSION class implemented by php.

This example describes 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:

<? 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
{
/**
* Default session validity period
* @ 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, '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.

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

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.