Store session in database

巴扎黑
Release: 2016-11-10 13:43:16
Original
999 people have browsed it

Sql statement code

CREATE TABLE sessions (  
  id CHAR(32) NOT NULL,  
  data TEXT,  
  last_accessed TIMESTAMP NOT NULL,  
  PRIMARY KEY (id)  
);
Copy after login

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();
Copy after login

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;;  
?>
Copy after login


Related labels:
sql
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template