Php: Writing user information to database _ PHP Tutorial

Source: Internet
Author: User
Php writes user information to the database. Php writes user information to database session advanced application writes user information to the database first creates a database table and creates a session table in the lab database sqldb to store data. php writes user information to the database.

Session advanced application writes user information to the database

First, create a database table.

Create a session table in the lab database sqldb to store data.

Create the required files in the root directory (focus on session and class. php class files, including some column methods)

In session. class. php, session_set_save_handler () is used to write data into database tables by using PDO,

Class defines some static methods, and its attributes must also be static, so that session data is directly written to the database, rather than stored in a local folder.

First, create a Session class. the class first defines some private and static attributes, and defines the ip address, survival time, and time.

 
Private static function init ($ handler) {self: $ handler = $ handler; // represents the PDO link. // The ip address cannot be empty. self: $ ip =! Empty ($ _ SERVER ["REMOTE_ADDR"])? $ _ SERVER ["REMOTE_ADDR"]: 'unkown '; // Retrieve the survival time from the configuration file self: $ lifetime = ini_get ('session. gc_maxlifetime '); self: $ time = time ();}

Next, define the method for enabling the session.

// Define the method for enabling session static function start (PDO $ pdo) {self: init ($ pdo); // initialize the private method session_set_save_handler (array (_ CLASS __, "open"), array (_ CLASS __, "close"), array (_ CLASS __, "read"), array (_ CLASS __, "write"), array (_ CLASS __, "destroy"), array (_ CLASS __, "gc"); session_start ();}

Open, close, read, write, destory, and gc methods are available when the session is enabled. the following mainly defines these methods:

Open () and close () methods

public static function open($path, $name){    return true;}public static function close(){    return true;}

When defining these methods, the most important are the write () and read () methods, because this is read or written directly from the database, using the PDO database preprocessing method

Read (): perform PDO preprocessing first. Then, in the obtained record, determine whether the ip address is the ip address in the database and whether the retrieved data has expired.

Public static function read ($ PHPSESSID) {$ SQL = "select PHPSESSID, update_time, client_ip, data from session where PHPSESSID =? "; // Use? Parameter // PDO preprocessing $ stmt = self: $ handler-> prepare ($ SQL); $ stmt-> execute (array ($ PHPSESSID )); // Obtain a record if (! $ Result = $ stmt-> fetch (PDO: FETCH_ASSOC) {return '';} // checks whether the current ip address is an ip address in the database if (self :: $ ip! = $ Result ["client_ip"]) {self: destroy ($ PHPSESSID); // destroy user return '';} // determine if it is an expired if ($ result ["update_time"] + self: $ lifetime) <self ::$ time) {self: destroy ($ PHPSESSID ); return '';} return $ result ['data']; // read successfully}


Write (): the same writing method is also important. you need to determine whether the data passed in is null. if it is null, no insert is performed.

Public static function write ($ PHPSESSID, $ data) {$ SQL = "select PHPSESSID, update_time, client_ip, data from session where PHPSESSID =? "; $ Stmt = self: $ handler-> prepare ($ SQL); $ stmt-> execute (array ($ PHPSESSID )); if ($ result = $ stmt-> fetch (PDO: FETCH_ASSOC) {// delay 30 update if ($ result ['data']! = $ Data | self: $ time> ($ result ['update _ time'] + 30 )) {// update data Statement $ SQL = "uptate session set update_time = ?, Data =? Where PHPSESSID =? "; $ Stm = self: $ handler-> prepare ($ SQL); $ stm-> execute (array (self: $ time, $ data, $ PHPSESSID) ;}} else {// determines whether the passed data is null. if (! Empty ($ data) {$ SQL = "insert into session (PHPSESSID, update_time, client_ip, data) values (?,?,?,?) "; // Use the insert value? Parameter $ Something = self ::$ handler-> prepare ($ SQL); $ Something-> execute (array ($ PHPSESSID, self :$ time, self ::$ ip, $ data); // An array must be used} return true ;}

The next step is data destruction.

Same destory () and gc ()

Destory (): delete data

Gc (): garbage collection

    public static function destroy($PHPSESSID){    $sql="delete from session where PHPSESSID=?";    $stmt=self::$handler->prepare($sql);    $stmt->execute(array($PHPSESSID));    return true;    }    private static function gc($lifetime){    $sql="delete from session where update_time < ?";    $stmt=self::$handler->prepare($sql);    $stmt->execute(array(self::$time-$lifetime));    return true;    }    }

Finally, an exception is thrown and the session class is called.

Try {$ pdo = new PDO ("mysql: host = localhost; dbname = sqldb", "root", "heyifeng19930924");} catch (PDOException $ e) {echo $ e-> getMessage () ;}// call the session class Session: start ($ pdo );


In the test file, the write method is the same as the session advanced usage (that is, the test file of the previous blog ).

Only include this class file in the contained file

Include "session. class. php ";

Test results: if the data is successfully inserted, the following table information is displayed in the database:

That is, pass the PHPSESSID value of the column

After the deletion is canceled, the query table is displayed.

That is, the PHPSESSID value is revoked.

The reset session advanced application writes user information to the database. First, create a database table and create a session table in the lab database sqldb to store data...

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.