Write Session to database, and write Session to database _ PHP Tutorial

Source: Internet
Author: User
Write sessions to the database, and write sessions to the database. Write the Session to the database, and write the Session to the database using the session_set_save_handler () function to write the Session content to the database 1? Php2 * 3 * @ authorFahy4 * @ linkhome. cnb writes the Session to the database, and the Session is written to the database.

Use the session_set_save_handler () function to write Session content to the database.

1
 Prepare ($ SQL); 47 $ stmt-> execute (array ($ PHPSESSID); 48 if (! $ Result = $ stmt-> fetch (PDO: FETCH_ASSOC) {49 return ''; 50} 51 if (self :: $ ip = $ result ['Client _ IP']) {52 self: destroy ($ PHPSESSID); 53 return ''; 54} 55 if ($ result ['update _ time'] + self: $ lifetime)
 
  
Prepare ($ SQL); 68 $ stmt-> execute (array ($ PHPSESSID); 69 70 if ($ result = $ stmt-> fetch (PDO: FETCH_ASSOC )) {71 if ($ result ['data']! = $ Data | self: $ time> ($ result ['update _ time'] + 30) {72 $ SQL = "update session set update_time = ?, Data =? Where PHPSESSID =? "; 73 $ stmt = self: $ handler-> prepare ($ SQL); 74 $ stmt-> execute (array ($ self: $ time, $ data, $ PHPSESSID); 75} 76} else {77 if (! Empty ($ data) {78 try {79 $ SQL = "insert into session (PHPSESSID, update_time, client_ip, data) values (?,?,?,?) "; 80} catch (PDOException $ e) {81 echo $ e-> getMessage (); 82} 83 $ th = self: $ handler-> prepare ($ SQL ); 84 $ Something-> execute (array ($ PHPSESSID, self: $ time, self: $ ip, $ data); 85} 86} 87 return true; 88} 89 90 public static function destroy ($ PHPSESSID) {91 $ SQL = "delete from session where PHPSESSID =? "; 92 $ stmt = self: $ handler-> prepare ($ SQL); 93 $ stmt-> execute (array ($ PHPSESSID); 94 return true; 95} 96 public static function gc ($ lifetime) {97 $ SQL = "delete from session where update_time
  Prepare ($ SQL); 99 $ stmt-> execute (array (self: $ time-$ lifetime); 100 return true; 101} 102} 103 // use PDO to connect to the database 104 try {105 $ pdo = new PDO ("mysql: host = localhost; dbname = session", "root ", "hwj193"); 106} catch (PDOException $ e) {107 echo $ e-> getMessage (); 108} 109 // Pass database resource 110 Session :: start ($ pdo );
 

Sessions uses the session_set_save_handler () function to write Session content to database 1? Php 2/* 3 * @ author Fahy 4 * @ link http://home.cnb...

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.