Home  >  Article  >  Backend Development  >  Store session in database

Store session in database

巴扎黑
巴扎黑Original
2016-11-10 13:43:16990browse

Sql statement code

CREATE TABLE sessions (  
  id CHAR(32) 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: 下午2:57  
 */  
$sdbc = NULL;  
  
function open_session() {  
    global $sdbc;  
    $sdbc = mysqli_connect(&#39;192.168.31.172&#39;, &#39;root&#39;, &#39;root&#39;, &#39;phpadvanced&#39;);  
    return true;  
}  
  
function close_session() {  
    global $sdbc;  
    return mysqli_close($sdbc);  
}  
  
function read_session($sid) {  
    global $sdbc;  
    $q = sprintf(&#39;SELECT data FROM sessions WHERE  id="%s"&#39;, 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 &#39;&#39;;  
    }  
}  
  
function write_session($sid, $data) {  
    global $sdbc;  
  
    $q = sprintf(&#39;REPLACE INTO sessions (id, data) VALUES ("%s", "%s")&#39;, mysqli_real_escape_string($sdbc, $sid), mysqli_real_escape_string($sdbc, $data));  
    $r = mysqli_query($sdbc, $q);  
    return true;  
}  
  
function destroy_session($sid) {  
    global $sdbc;  
    $q = sprintf(&#39;DELETE FROM session WHERE id="%s"&#39;, mysqli_real_escape_string($sdbc, $sid));  
    $r = mysqli_query($sdbc, $q);  
  
    $_SESSION = array();  
  
    return true;  
}  
  
function clean_session($expire) {  
    global $sdbc;  
  
    $q = sprintf(&#39;DELETE FROM sessions WHERE DATE_ADD(last_accessed, INTERVAL %d SECOND) < NOW()&#39;, (int)$expire);  
    $r = mysqli_query($sdbc, $q);  
  
    return true;  
}  
  
session_set_save_handler(&#39;open_session&#39;, &#39;close_session&#39;, &#39;read_session&#39;, &#39;write_session&#39;, &#39;destroy_session&#39;, &#39;clean_session&#39;);  
  
session_start();

Call code on a normal page

  
    
    
    colour_blue  
      
      
      
        
    
<?php  
if(empty($_SESSION)) {  
    $_SESSION[&#39;blah&#39;] = &#39;umlaut&#39;;  
    $_SESSION[&#39;this&#39;] = 3615684.45;  
    $_SESSION[&#39;that&#39;] = &#39;blue&#39;;  
    echo &#39;Session已存储&#39;;  
} else {  
    echo &#39;SESSION已存在, &#39;.print_r($_SESSION, 1).&#39;&#39;;  
}  
  
if(isset($_GET[&#39;logout&#39;])) {  
    session_destroy();  
    echo &#39;会话结束&#39;;  
} else {  
    echo &#39;登出&#39;;  
}  
  
echo &#39;会话数据: &#39;.print_r($_SESSION, 1).&#39;&#39;;  
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Yii2 set time zoneNext article:Yii2 set time zone