本文实例讲述了SESSION存放在数据库用法。分享给大家供大家参考。具体如下:

<"session.gc_maxlifetime");
 // open database-connection
 $dbHandle = @mysql_connect("localhost","root","");
 $dbSel = @mysql_select_db("test",$dbHandle);
 // return success
 if(!$dbHandle || !$dbSel)
  return false;
 $this->dbHandle = $dbHandle;
 return true;
 }
 function close() {
 $this->gc(ini_get('session.gc_maxlifetime'));
 // close database-connection
 return @mysql_close($this->dbHandle);
 }
 function read($sessID) {
 // fetch session-data
 $res = mysql_query("SELECT session_data AS d FROM ws_sessions
    WHERE session_id = '$sessID'
    AND session_expires > ".time(),$this->dbHandle);
 // return data or an empty string at failure
 if($row = mysql_fetch_assoc($res))
  return $row['d'];
 return "";
 }
 function write($sessID,$sessData) {
 // new session-expire-time
 $newExp = time() + $this->lifeTime;
 // is a session with this id in the database"SELECT * FROM ws_sessions
    WHERE session_id = '$sessID'",$this->dbHandle);
 // if yes,
 if(mysql_num_rows($res)) {
  // ...update session-data
  mysql_query("UPDATE ws_sessions
    SET session_expires = '$newExp',
    session_data = '$sessData'
    WHERE session_id = '$sessID'",$this->dbHandle);
  // if something happened, return true
  if(mysql_affected_rows($this->dbHandle))
  return true;
 }
 // if no session-data was found,
 else {
  // create a new row
  mysql_query("INSERT INTO ws_sessions (
    session_id,
    session_expires,
    session_data)
    VALUES(
    '$sessID',
    '$newExp',
    '$sessData')",$this->dbHandle);
  // if row was created, return true
  if(mysql_affected_rows($this->dbHandle))
  return true;
 }
 // an unknown error occured
 return false;
 }
 function destroy($sessID) {
 // delete session-data
 mysql_query("DELETE FROM ws_sessions WHERE session_id = '$sessID'",$this->dbHandle);
 // if session was deleted, return true,
 if(mysql_affected_rows($this->dbHandle))
  return true;
 // ...else return false
 return false;
 }
 function gc($sessMaxLifeTime) {
 // delete old sessions
 mysql_query("DELETE FROM ws_sessions WHERE session_expires < ".time(),$this->dbHandle);
 // return affected rows
 return mysql_affected_rows($this->dbHandle);
 }
}
$session = new session();
session_set_save_handler(array(&$session,"open"),
    array(&$session,"close"),
    array(&$session,"read"),
    array(&$session,"write"),
    array(&$session,"destroy"),
    array(&$session,"gc"));
session_start();
// etc...
?>

希望本文所述对大家的php程序设计有所帮助。

标签:
SESSION,存放,数据库

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

评论“SESSION存放在数据库用法实例”

暂无“SESSION存放在数据库用法实例”评论...

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。