Maison  >  Article  >  développement back-end  >  将session存储到数据库中

将session存储到数据库中

巴扎黑
巴扎黑original
2016-11-10 13:43:16992parcourir

Sql语句代码  

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

自定义会话处理代码  

<?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();

 

在普通页面调用代码  

  
    
    
    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;;  
?>


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:Yii2设置时区Article suivant:printf和sprintf的应用