中文字幕免费精品_亚洲视频自拍_亚洲综合国产激情另类一区_色综合咪咪久久

php獨立session數據庫存儲操作類分享
來源:易賢網 閱讀:1246 次 日期:2014-10-08 13:31:21
溫馨提示:易賢網小編為您整理了“php獨立session數據庫存儲操作類分享”,方便廣大網友查閱!

直接上代碼:

代碼如下:

class dbsession

{

const type_int = 1;

const type_str = 2;

/**

* database configration

*

* @var array

*/

private $_config = array(

‘host' => '127.0.0.1′,

‘port' => 3306,

‘username' => ‘root',

‘password' => ‘root',

‘dbname' => ‘db_mylab',

‘tablename' => ‘t_sessions',

‘cookie_prefix' => ‘mylab_',

‘cookiepath' => ‘/',

‘cookiedomain' => ”,

‘cookie_timeout' => 900

);

/**

* table fields type array

*

* @var array

*/

private $_db_fields = array(

‘crc32sid' => self::type_int,

‘sessionhash' => self::type_str,

‘idhash' => self::type_str,

‘userid' => self::type_int,

‘ipaddress' => self::type_str,

‘lastactivity' => self::type_str,

‘location' => self::type_str,

‘loggedin' => self::type_int,

‘heartbeat' => self::type_str

);

/**

* db obj

*

* @var mysqli object

*/

private $_mysqli = null;

/**

* weather the session was created or existed previously

*

* @var bool

*/

private $_created = false;

/**

* array of changes.

*

* @var array

*/

private $_changes = array();

/**

* @var bool

*/

private $_db_inited = false;

/**

* session host

*

* @var string

*/

private $_session_host = ”;

/**

* session idhash

*

* @var string

*/

private $_session_idhash = ”;

private $_dbsessionhash = ”;

private $_vars = array();

public function __construct()

{

$this->_dbsessionhash = addslashes($this->get_cookie(‘sessionhash'));

$this->_session_host = substr($_server[‘remote_addr'], 0, 15);

#this should *never* change during a session

$this->_session_idhash = md5($_server[‘http_user_agent'] . self::fetch_substr_ip(self::fetch_alt_ip()) );

$this->_init_config();

$this->init_db();

$gotsession = false;

if ($this->_dbsessionhash)

{

$sql = ‘

select *

from ‘ . $this->_config[‘tablename'] . ‘

where crc32sid = ‘ . sprintf(‘%u', crc32($this->_dbsessionhash)) . ‘

and sessionhash = '‘ . $this->_dbsessionhash . ‘'

and idhash = '‘ . $this->_session_idhash . ‘'

and heartbeat > '‘ . date(‘y-m-d h:i:s' ,timenow – $this->_config[‘cookie_timeout']) . ‘'‘;

//echo $sql;exit;

$result = $this->_mysqli->query($sql);

$session = $result->fetch_array(mysqli_assoc);

if ($session and ($this->fetch_substr_ip($session[‘ipaddress']) == $this->fetch_substr_ip($this->_session_host)))

{

$gotsession = true;

$this->_vars = $session;

$this->_created = false;

}

}

if ($gotsession == false)

{

$this->_vars = $this->fetch_session();

$this->_created = true;

$gotsession = true;

}

if ($this->_created == false)

{

$this->set(‘lastactivity', date(‘y-m-d h:i:s', timenow));

$this->set(‘location', $_server[‘request_uri']);

}

}

/**

* builds an array that can be used to build a query to insert/update the session

*

* @return array array of column name => prepared value

*/

private function _build_query_array()

{

$return = array();

foreach ($this->_db_fields as $fieldname => $cleantype)

{

switch ($cleantype)

{

case self::type_int:

$cleaned = is_numeric($this->_vars[$fieldname]) ? $this->_vars[$fieldname] : intval($this->_vars[$fieldname]);

break;

case self::type_str:

default:

$cleaned = ' . addslashes($this->_vars[$fieldname]) . ';

}

$return[$fieldname] = $cleaned;

}

return $return;

}

/**

* sets a session variable and updates the change list.

*

* @param string name of session variable to update

* @param string value to update it with

*/

public function set($key, $value)

{

if ($this->_vars[$key] != $value)

{

$this->_vars[$key] = $value;

$this->_changes[$key] = true;

}

}

public function get($key)

{

return $this->_vars[$key];

}

public function unsetchangedvar($var)

{

if (isset($this->_changes[$var]))

{

unset($this->_changes[$var]);

}

}

/**

* fetches a valid sessionhash value, not necessarily the one tied to this session.

*

* @return string 32-character sessionhash

*/

static function fetch_sessionhash()

{

return hash(‘md5′ , timenow . rand(1, 100000) . uniqid() );

}

private function _init_config()

{

$registry = zend_registry::getinstance();

$config = $registry->get(‘config');

$this->_config[‘host'] = $config->database->params->host;

$this->_config[‘port'] = $config->database->params->port;

$this->_config[‘username'] = $config->database->params->username;

$this->_config[‘password'] = $config->database->params->password;

$this->_config[‘dbname'] = $config->database->params->dbname;

$this->_config[‘tablename'] = $config->database->session->tablename;

}

/**

* initialize database connection

*/

public function init_db()

{

if ($this->_db_inited)

{

return true;

}

//mysqli_report(mysqli_report_off);

$this->_mysqli = new mysqli(

$this->_config[‘host'],

$this->_config[‘username'],

$this->_config[‘password'],

$this->_config[‘dbname'],

$this->_config[‘port']

);

/* check connection */

if (mysqli_connect_errno())

{

// printf(connect failed: %sn, mysqli_connect_error());

// echo ‘in ‘, __file__, ‘ on line ‘, __line__;

echo { success: false, errors: { reason: ‘ connect failed: . addslashes( mysqli_connect_error() ) . ' }};

exit();

}

$this->_mysqli->query(‘set names latin1′);

$this->_db_inited = true;

return true;

}

/**

* fetches an alternate ip address of the current visitor, attempting to detect proxies etc.

*

* @return string

*/

static function fetch_alt_ip()

{

$alt_ip = $_server[‘remote_addr'];

if (isset($_server[‘http_client_ip']))

{

$alt_ip = $_server[‘http_client_ip'];

}

else if (isset($_server[‘http_from']))

{

$alt_ip = $_server[‘http_from'];

}

return $alt_ip;

}

/**

* returns the ip address with the specified number of octets removed

*

* @param string ip address

*

* @return string truncated ip address

*/

static function fetch_substr_ip($ip, $length = null)

{

return implode(‘.', array_slice(explode(‘.', $ip), 0, 4 – $length));

}

/**

* fetches a default session. used when creating a new session.

*

* @param integer user id the session should be for

*

* @return array array of session variables

*/

public function fetch_session($userid = 0)

{

$sessionhash = self::fetch_sessionhash();

$this->set_cookie(‘sessionhash', $sessionhash);

return array(

‘crc32sid' => sprintf(‘%u', crc32($sessionhash)),

‘sessionhash' => $sessionhash,

‘idhash' => $this->_session_idhash,

‘userid' => $userid,

‘ipaddress' => $this->_session_host,

‘lastactivity' => date(‘y-m-d h:i:s', timenow),

‘location' => $_server[‘request_uri'],

‘loggedin' => $userid ? 1 : 0,

‘heartbeat' => date(‘y-m-d h:i:s', timenow)

);

}

public function get_cookie($cookiename)

{

$full_cookiename = $this->_config[‘cookie_prefix'] . $cookiename;

if (isset($_cookie[$full_cookiename]))

{

return $_cookie[$full_cookiename];

}

else

{

return false;

}

}

public function set_cookie($name, $value = ”, $permanent = 1, $allowsecure = true)

{

if ($permanent)

{

$expire = timenow + 60 * 60 * 24 * 365;

}

else

{

$expire = 0;

}

if ($_server[‘server_port'] == '443′)

{

// we're using ssl

$secure = 1;

}

else

{

$secure = 0;

}

// check for ssl

$secure = ((req_protocol === ‘https' and $allowsecure) ? true : false);

$name = $this->_config[‘cookie_prefix'] . $name;

$filename = ‘n/a';

$linenum = 0;

if (!headers_sent($filename, $linenum))

{ // consider showing an error message if there not sent using above variables?

if ($value == ” and strlen($this->_config[‘cookiepath']) > 1 and strpos($this->_config[‘cookiepath'], ‘/') !== false)

{

// this will attempt to unset the cookie at each directory up the path.

// ie, cookiepath = /test/abc/. these will be unset: /, /test, /test/, /test/abc, /test/abc/

// this should hopefully prevent cookie conflicts when the cookie path is changed.

$dirarray = explode(‘/', preg_replace(‘#/+$#', ”, $this->_config[‘cookiepath']));

$alldirs = ”;

foreach ($dirarray as $thisdir)

{

$alldirs .= $thisdir;

if (!empty($thisdir))

{ // try unsetting without the / at the end

setcookie($name, $value, $expire, $alldirs, $this->_config[‘cookiedomain'], $secure);

}

$alldirs .= /;

setcookie($name, $value, $expire, $alldirs, $this->_config[‘cookiedomain'], $secure);

}

}

else

{

setcookie($name, $value, $expire, $this->_config[‘cookiepath'], $this->_config[‘cookiedomain'], $secure);

}

}

else if (!debug)

{

echo can't set cookies;

}

}

private function _save()

{

$cleaned = $this->_build_query_array();

if ($this->_created)

{

//var_dump($cleaned);

# insert query

$this->_mysqli->query(‘

insert ignore into ‘ . $this->_config[‘tablename'] . ‘

(‘ . implode(‘,', array_keys($cleaned)) . ‘)

values

(‘ . implode(‘,', $cleaned). ‘)

‘);

}

else

{

# update query

$update = array();

foreach ($cleaned as $key => $value)

{

if (!empty($this->_changes[$key]))

{

$update[] = $key = $value;

}

}

if (sizeof($update) > 0)

{

$sql = ‘update ‘ . $this->_config[‘tablename'] . ‘

set ‘ . implode(‘, ‘, $update) . ‘

where crc32sid = ‘ . sprintf(‘%u', crc32($this->_dbsessionhash)) . ‘

and sessionhash = '‘ . $this->_dbsessionhash . ‘'‘;

//echo $sql;

$this->_mysqli->query($sql);

}

}

}

public function getonlineusernum()

{

$sql = ‘

select count(*) as cnt

from ‘ . $this->_config[‘tablename'] . ‘

where loggedin = 1

and heartbeat > '‘ . date(‘y-m-d h:i:s' ,timenow – $this->_config[‘cookie_timeout']) . ‘'‘;

$result = $this->_mysqli->query($sql);

$row = $result->fetch_array(mysqli_assoc);

return $row[‘cnt'];

}

private function _gc()

{

$rand_num = rand(); # randow integer between 0 and getrandmax()

if ($rand_num < 100)

{

$sql = ‘delete from ‘ . $this->_config[‘tablename'] . ‘

where heartbeat < '‘ . date(‘y-m-d h:i:s' ,timenow – $this->_config[‘cookie_timeout']) . ‘'‘;

$this->_mysqli->query($sql);

}

}

public function __destruct()

{

$this->_save();

$this->_gc();

$this->_mysqli->close();

}

}

更多信息請查看IT技術專欄

更多信息請查看網絡編程
由于各方面情況的不斷調整與變化,易賢網提供的所有考試信息和咨詢回復僅供參考,敬請考生以權威部門公布的正式信息和咨詢為準!

2026上岸·考公考編培訓報班

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
云南網警備案專用圖標
聯系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網
云南網警報警專用圖標
中文字幕免费精品_亚洲视频自拍_亚洲综合国产激情另类一区_色综合咪咪久久
欧美视频官网| 欧美一区二区三区视频免费播放| 久久夜色精品一区| 久久婷婷综合激情| 欧美96在线丨欧| 欧美午夜www高清视频| 欧美性开放视频| 国内精品久久久久影院 日本资源 国内精品久久久久伊人av | 亚洲欧美国产不卡| 老牛嫩草一区二区三区日本| 欧美日韩免费一区| 国产亚洲欧美一区二区三区| 日韩亚洲欧美一区二区三区| 久久本道综合色狠狠五月| 久久久综合免费视频| 欧美色区777第一页| 一区二区三区在线不卡| 亚洲视频 欧洲视频| 久久精视频免费在线久久完整在线看| 欧美成人小视频| 国产欧美在线| 亚洲字幕在线观看| 欧美三级乱人伦电影| 亚洲美女色禁图| 欧美3dxxxxhd| 精品成人久久| 麻豆精品在线视频| 国产一区二区三区在线免费观看| 亚洲图片激情小说| 欧美成人综合| 亚洲国产一区二区三区青草影视 | 久色成人在线| 国产日韩欧美一区二区三区在线观看 | 国模套图日韩精品一区二区| 亚洲免费观看在线观看| 久久久亚洲国产天美传媒修理工 | 最新中文字幕一区二区三区| 亚洲一区二区三区在线看| 久久婷婷久久| 欧美日韩高清免费| 亚洲精选大片| 欧美高潮视频| 在线观看日韩| 久久久久久久久伊人| 一区二区三区在线观看视频| 亚洲深夜影院| 国产精品免费福利| 亚洲欧美国产77777| 一本色道久久88亚洲综合88| 欧美日韩一二区| 亚洲自拍偷拍一区| 国产视频在线观看一区| 久久在线精品| 亚洲国产专区校园欧美| 欧美精品粉嫩高潮一区二区| 中日韩视频在线观看| 国产亚洲综合精品| 免费视频亚洲| 亚洲深夜福利视频| 国产欧美不卡| 欧美一区二区三区婷婷月色| 国产日韩欧美另类| 你懂的国产精品永久在线| 在线性视频日韩欧美| 国产精品免费一区二区三区在线观看 | 亚洲精品美女在线观看| 欧美日韩亚洲一区二| 亚洲一区在线播放| 在线不卡视频| 国产日韩欧美精品在线| 欧美日韩国产精品成人| 亚洲在线观看| 在线播放日韩专区| 国产精品免费aⅴ片在线观看| 久久精品欧洲| aa国产精品| 在线观看精品一区| 国产精品扒开腿做爽爽爽软件| 亚洲欧美日韩精品久久亚洲区 | 国产精品一区视频网站| 久久久欧美一区二区| 亚洲丝袜av一区| 亚洲精品系列| 亚洲福利专区| 国模吧视频一区| 欧美特黄一级| 欧美1区2区| 久久久久久欧美| 欧美一区二区女人| 一区二区国产日产| 一本不卡影院| 亚洲高清视频在线| 国产一区二区视频在线观看 | 亚洲国产精品成人一区二区| 欧美日韩综合| 欧美理论电影网| 免费av成人在线| 久久久999| 亚洲一区日韩在线| 亚洲精品在线一区二区| 伊人夜夜躁av伊人久久| 国内精品久久久久久影视8 | 亚洲精品综合久久中文字幕| 国产在线成人| 国产精品欧美日韩久久| 欧美国产日本高清在线| 久久影院午夜片一区| 亚洲欧美另类久久久精品2019| 一区二区三区免费在线观看| 日韩一区二区电影网| 136国产福利精品导航网址应用| 国产精品香蕉在线观看| 欧美性大战久久久久久久蜜臀| 欧美女同在线视频| 欧美日韩不卡在线| 欧美午夜精品久久久久久孕妇| 国产精品丝袜91| 欧美激情亚洲另类| 在线综合+亚洲+欧美中文字幕| 亚洲人成网站在线播| 亚洲欧洲综合| 99国产精品自拍| 一区二区高清视频在线观看| 99精品久久久| 亚洲色图综合久久| 亚洲中无吗在线| 午夜视频久久久| 久久久人成影片一区二区三区| 美女主播视频一区| 欧美日韩直播| 国产精品自拍一区| 亚洲第一级黄色片| 日韩视频免费观看高清完整版| 亚洲免费一区二区| 免播放器亚洲| 国产精品男女猛烈高潮激情 | 久久久综合精品| 免费一级欧美片在线播放| 欧美日韩国产一区二区三区地区| 欧美视频福利| 国产丝袜美腿一区二区三区| 亚洲国产精品成人一区二区 | 亚洲精品美女91| 亚洲一区二区三区乱码aⅴ蜜桃女 亚洲一区二区三区乱码aⅴ | 亚洲欧美综合网| 久久精品夜色噜噜亚洲a∨| 欧美aaaaaaaa牛牛影院| 国产精品成人播放| 亚洲成人在线网站| 午夜精品一区二区三区在线播放 | 一区二区成人精品| 欧美在线视频免费播放| 欧美精品在线极品| 一区一区视频| 久久国产直播| 国产精品久久久久久久午夜| 亚洲日本中文| 免费久久99精品国产自在现线| 国产精品久久二区| 亚洲免费观看在线观看| 欧美成人亚洲成人| 伊人春色精品| 久久久久青草大香线综合精品| 国产精品草草| 亚洲黄色尤物视频| 久久久精品欧美丰满| 国产欧美欧美| 午夜精品一区二区三区在线视 | 欧美一级电影久久| 欧美韩日视频| 亚洲高清免费| 欧美一区二区三区视频免费播放 | 欧美中在线观看| 国产精品久久国产三级国电话系列| 亚洲国产精品视频| 理论片一区二区在线| 国产一区二区三区观看| 欧美一区二区视频观看视频| 国产精品自拍一区| 欧美一站二站| 激情欧美一区二区三区在线观看| 亚洲天天影视| 国产精品福利在线观看| 中文日韩在线| 国产精品va在线播放我和闺蜜| 日韩视频二区| 欧美日韩在线播放| 亚洲影院在线| 国产亚洲精品久久久久久| 欧美一区国产二区| 狠狠色伊人亚洲综合成人| 久久久一区二区| 亚洲久久成人| 国产精品久久久久99| 久久精品色图| 99精品国产福利在线观看免费| 欧美午夜一区二区| 亚洲综合色在线| 国内精品久久久久久影视8| 久久夜色精品亚洲噜噜国产mv|