ob_end_clean();

if((int)ini_get('session.auto_start') > 0)

{

ini_set('session.auto_start',0);

}

function zen_session_open()

{

global $zen_session_db;

$zen_session_db = new mysqli('localhost','root','wh36524','phptest');

return TRUE;

}

function zen_session_close()

{

global $zen_session_db;

return $zen_session_db -> close();

}

function zen_session_read($sid)

{

global $zen_session_db;

$sql = 'select session_value from sessions where session_id="'.$sid.'"';

$query = $zen_session_db->query($sql);

if($zen_session_db -> errno)

{

die($zen_session_db->error);

}

if($query -> num_rows == 1)

{

$data = $query->fetch_assoc();

return $data['session_value'];

}

else

{

return "";

}

}

function zen_session_write($sid,$data)

{

global $zen_session_db;

$sql = 'select count(*) from sessions where session_id="'.$sid.'"';

$query = $zen_session_db->query($sql);

if($zen_session_db->errno)

{

die($zen_session_db->error);

}

if($query->num_rows > 0)

{

$sql = 'update session set session_value="'.$data.'",expire='.(time()+60*60).' where session_id="'.

$sid.'"';

$zen_session_db->query($sql);

}

else

{

$sql = 'insert into session values("'.$sid.'","'.(time()+60*60).'","'.$data.'")';

$zen_session_db->query($sql);

}

/*

if($zen_session_db->affected_rows > 0)

{

return TRUE;

}

else

{

return FALSE;

}

*/

return TRUE;

}

function zen_session_destroy($sid)

{

global $zen_session_db;

$sql = 'delete from session where session_id="'.$sid.'"';

$zen_session_db->query($sql);

$_SESSION = array();

return TRUE;

}

function zen_session_gc($time)

{

global $zen_session_db;

$sql = 'delete from session where expire < '.time();

$zen_session_db->query($sql);

return TRUE;

}

session_set_save_handler('zen_session_open','zen_session_close','zen_session_read','zen_session_write',

'zen_session_destroy','zen_session_gc'

);

session_name('zenID');

session_start();

$_SESSION['age'] = 16;

$_SESSION['color'] = 'green';

session_write_close();

不知道怎么回事,比到参考书写的,但是就是无法实现在数据库存储啊,我在另外一个页面拷贝了上面的代码,然后打印$_SESSION数组,但是是空的,不知道是哪里的问题?

ob_end_clean();

if((int)ini_get('session.auto_start') > 0)

{

ini_set('session.auto_start',1);

}

function zen_session_open()

{

global $zen_session_db;

$zen_session_db = new mysqli('localhost','root','wh36524','phptest');

return TRUE;

}

function zen_session_close()

{

global $zen_session_db;

return $zen_session_db -> close();

}

function zen_session_read($sid)

{

global $zen_session_db;

$sql = 'select session_value from sessions where session_id="'.$sid.'"';

$query = $zen_session_db->query($sql);

if($zen_session_db -> errno)

{

die($zen_session_db->error);

}

if($query -> num_rows == 1)

{

$data = $query->fetch_assoc();

return $data['session_value'];

}

else

{

return "";

}

}

function zen_session_write($sid,$data)

{

global $zen_session_db;

$sql = 'select count(*) from sessions where session_id="'.$sid.'"';

$query = $zen_session_db->query($sql);

if($zen_session_db->errno)

{

die($zen_session_db->error);

}

if($query->num_rows > 0)

{

$sql = 'update session set session_value="'.$data.'",expire='.(time()+60*60).' where session_id="'.

$sid.'"';

$zen_session_db->query($sql);

}

else

{

$sql = 'insert into session values("'.$sid.'","'.(time()+60*60).'","'.$data.'")';

$zen_session_db->query($sql);

}

/*

if($zen_session_db->affected_rows > 0)

{

return TRUE;

}

else

{

return FALSE;

}

*/

return TRUE;

}

function zen_session_destroy($sid)

{

global $zen_session_db;

$sql = 'delete from session where session_id="'.$sid.'"';

$zen_session_db->query($sql);

$_SESSION = array();

return TRUE;

}

function zen_session_gc($time)

{

global $zen_session_db;

$sql = 'delete from session where expire < '.time();

$zen_session_db->query($sql);

return TRUE;

}

session_set_save_handler('zen_session_open','zen_session_close','zen_session_read','zen_session_write',

'zen_session_destroy','zen_session_gc'

);

session_name('zenID');

session_start();

//$_SESSION['age'] = 16;

//$_SESSION['color'] = 'green';

print_r($_SESSION);

session_write_close();

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐