Store the session in a database

Source: Internet
Author: User
SQL statement Code

CREATE TABLE sessions (    ID CHAR (+) NOT NULL,    data TEXT,    last_accessed TIMESTAMP not NULL,    PRIMARY KEY (ID )  );

Custom session Processing Code

<?php/** * Created by Phpstorm.    * User:michaeldu * DATE:15/7/14 * Time: PM 2:57 */$sdbc = NULL;      function open_session () {Global $sdbc;      $SDBC = Mysqli_connect (' 192.168.31.172 ', ' root ', ' root ', ' phpadvanced ');  return true;      } function Close_session () {Global $sdbc;  Return Mysqli_close ($SDBC);      } function Read_session ($SID) {global $sdbc;      $q = sprintf (' SELECT data from Sessions WHERE id= '%s ', mysqli_real_escape_string ($SDBC, $sid));        $r = Mysqli_query ($sdbc, $q);          if (mysqli_num_rows ($r) = = 1) {list ($data) = Mysqli_fetch_array ($r, mysqli_num);      return $data;      } else {return ';        }} function Write_session ($sid, $data) {global $sdbc; $q = sprintf (' REPLACE into sessions (ID, data) VALUES ('%s ', '%s ') ', mysqli_real_escape_string ($SDBC, $sid), mysqli_real_e      Scape_string ($SDBC, $data));      $r = Mysqli_query ($sdbc, $q);  return true; } function Destroy_sessiOn ($SID) {global $sdbc;      $q = sprintf (' DELETE from session WHERE id= '%s ', mysqli_real_escape_string ($SDBC, $sid));        $r = Mysqli_query ($sdbc, $q);        $_session = Array ();  return true;        } function Clean_session ($expire) {global $sdbc;      $q = sprintf (' DELETE from Sessions "WHERE Date_add (last_accessed, INTERVAL%d SECOND) < now () ', (int) $expire);        $r = Mysqli_query ($sdbc, $q);  return true; } session_set_save_handler (' Open_session ', ' close_session ', ' read_session ', ' write_session ', ' destroy_session ', '    Clean_session '); Session_Start ();

Calling code on a normal page

              Colour_blue                                <?php  if (empty ($_session)) {      $_session[' blah '] = ' umlaut ';      $_session[' this '] = 3615684.45;      $_session[' that '] = ' blue ';      Echo ' session has been stored ';  } else {      echo ' SESSION already exists, '. Print_r ($_session, 1). ';  }    if (Isset ($_get[' logout ')) {      Session_destroy ();      Echo ' session ended ';  } else {      echo ' logout ';  }    Echo ' Session data: '. Print_r ($_session, 1). ";  >
  • 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.