session_start();
ini_set('session.save_path','/tmp/');
//6个钟头
ini_set('session.gc_maxlifetime',21600);
//保存一天
$lifeTime = 24 * 3600;
setcookie(session_name(), session_id(), time() + $lifeTime, "https://www.jb51.net/");
后记:
其实真正的永久储存是不可能的,因为Cookie的保存时间有限,而服务器的空间也有限……但是对于一些需要保存时间比较长的站点,以上方法就已经足够了!
把session放入mysql的Example:
数据库里建表:session ( sesskey varchar32 , expiry int11 , value longtext)
code:
代码执行前已经连接数据库了。
代码如下:
复制代码 代码如下:
define('STORE_SESSIONS','mysql');
if (STORE_SESSIONS == 'mysql') {
if (!$SESS_LIFE = get_cfg_var('session.gc_maxlifetime')) {
$SESS_LIFE = 1440;
}
function _sess_open($save_path, $session_name) {
// 如果没有连接数据库,可以在此执行mysql_pconnect,mysql_select_db
return true;
}
function _sess_close() {
return true;
}
function _sess_read($key) {
$value_query = mysql_query("select value from sessions where sesskey = '" .addslashes($key) . "' and expiry > '" . time() . "'");
$value = mysql_fetch_array($value_query);
if (isset($value['value'])) {
return $value['value'];
}
return false;
}
function _sess_write($key, $val) {
global $SESS_LIFE;
$expiry = time() + $SESS_LIFE;
$value = $val;
$check_query = mysql_query("select count(*) as total from sessions where sesskey = '" . addslashes($key) . "'");
$check = mysql_fetch_array($check_query);
if ($check['total'] > 0) {
return mysql_query("update sessions set expiry = '" . addslashes($expiry) . "', value = '" . addslashes($value) . "' where sesskey = '" . addslashes($key) . "'");
} else {
return mysql_query("insert into sessions values ('" . addslashes($key) . "', '" . addslashes($expiry) . "', '" . addslashes($value) . "')");
}
}
function _sess_destroy($key) {
return mysql_query("delete from sessions where sesskey = '" . addslashes($key) . "'");
}
function _sess_gc($maxlifetime) {
mysql_query("delete from sessions where expiry < '" . time() . "'");
return true;
}
session_set_save_handler('_sess_open', '_sess_close', '_sess_read', '_sess_write', '_sess_destroy', '_sess_gc');
}
danoo_session_name( 'dtvSid' );
danoo_session_save_path(SESSION_WRITE_DIRECTORY);
还是有点不明白,open,write那些参数哪里来的。
修改php.ini配置的两个常用函数:
get_cfg_var('session.gc_maxlifetime') : 取得session.gc_maxlifetime的值
ini_set('session.cookie_lifetime','0') : 设置session.cookie_lifetime的值为0。
您可能感兴趣的文章: