是不是应该把类里面的函数顺序改一下!
function unserializes($data_value) {
$vars = preg_split(
'/([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\|/',
$data_value, -1, PREG_SPLIT_NO_EMPTY |
PREG_SPLIT_DELIM_CAPTURE
);
for ($i = 0; isset($vars[$i]); $i++) {
$result[$vars[$i++]] = unserialize($vars[$i]);
}
return $result;
} function open($path, $name) {
return true;
}
function close() {
$this->gc($this->lasttime);
return true;
}
function read($SessionKey){
$sql = "SELECT uid FROM sessions WHERE session_id = '".$SessionKey."' limit 1";
$query =$this->db->query($sql);
if($row=$this->db->fetch_array($query)){
return $row['uid'];
}else{
return "";
}
}
function write($SessionKey,$VArray) { require_once(MRoot.DIR_WS_CLASSES .'db_mysql_class.php');
$db1=new DbCom();
// make a connection to the database... now
$db1->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE);
$db1->query("set names utf8");
$this->db=$db1;
$SessionArray = addslashes($VArray);
$data=$this->unserializes($VArray);
$sql0 = "SELECT uid FROM sessions WHERE session_id = '".$SessionKey."' limit 1";
$query0 =$this->db->query($sql0);
if($this->db->num_rows($query0)<=0){
if (isset($data['webid']) && !empty($data['webid'])) {
$this->db->query("insert into `sessions` set `session_id` = '$SessionKey',uid='".$data['webid']."',last_visit='".time()."'");
}
return true;
}else{
/*$sql = "update `sessions` set ";
if(isset($data['webid'])){
$sql .= "uid = '".$data['webid']."', " ;
}
$sql.="`last_visit` = null "
. "where `session_id` = '$SessionKey'";
$this->db->query($sql); */
return true;
}
}
function destroy($SessionKey) {
$this->db->query("delete from `sessions` where `session_id` = '$SessionKey'");
return true;
}
function gc($lifetime) {
$this->db->query("delete from `sessions` where unix_timestamp(now()) -`last_visit` > '".$this->lasttime."'");
return true;
}// 改后
function session(&$db) {
$this->db = &$db;
session_module_name('user'); //session文件保存方式,这个是必须的!除非在Php.ini文件中设置了
session_set_save_handler(
array(&$this, 'open'), //在运行session_start()时执行 array(&$this, 'close'), //在脚本执行完成或调用session_write_close() 或 session_destroy()时被执行,即在所有session操作完后被执行 array(&$this, 'read'), //在运行session_start()时执行,因为在session_start时,会去read当前session数据
array(&$this, 'write'), //此方法在脚本结束和使用session_write_close()强制提交SESSION数据时执行 array(&$this, 'destroy'), //在运行session_destroy()时执行 array(&$this, 'gc') //执行概率由session.gc_probability 和 session.gc_divisor的值决定,时机是在open,read之后,session_start会相继执行open,read和gc
);
session_start(); //这也是必须的,打开session,必须在session_set_save_handler后面执行
}
function unserializes($data_value) {
$vars = preg_split(
'/([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\|/',
$data_value, -1, PREG_SPLIT_NO_EMPTY |
PREG_SPLIT_DELIM_CAPTURE
);
for ($i = 0; isset($vars[$i]); $i++) {
$result[$vars[$i++]] = unserialize($vars[$i]);
}
return $result;
} function open($path, $name) {
return true;
}
function close() {
$this->gc($this->lasttime);
return true;
}
function read($SessionKey){
$sql = "SELECT uid FROM sessions WHERE session_id = '".$SessionKey."' limit 1";
$query =$this->db->query($sql);
if($row=$this->db->fetch_array($query)){
return $row['uid'];
}else{
return "";
}
}
function write($SessionKey,$VArray) { require_once(MRoot.DIR_WS_CLASSES .'db_mysql_class.php');
$db1=new DbCom();
// make a connection to the database... now
$db1->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE);
$db1->query("set names utf8");
$this->db=$db1;
$SessionArray = addslashes($VArray);
$data=$this->unserializes($VArray);
$sql0 = "SELECT uid FROM sessions WHERE session_id = '".$SessionKey."' limit 1";
$query0 =$this->db->query($sql0);
if($this->db->num_rows($query0)<=0){
if (isset($data['webid']) && !empty($data['webid'])) {
$this->db->query("insert into `sessions` set `session_id` = '$SessionKey',uid='".$data['webid']."',last_visit='".time()."'");
}
return true;
}else{
/*$sql = "update `sessions` set ";
if(isset($data['webid'])){
$sql .= "uid = '".$data['webid']."', " ;
}
$sql.="`last_visit` = null "
. "where `session_id` = '$SessionKey'";
$this->db->query($sql); */
return true;
}
}
function destroy($SessionKey) {
$this->db->query("delete from `sessions` where `session_id` = '$SessionKey'");
return true;
}
function gc($lifetime) {
$this->db->query("delete from `sessions` where unix_timestamp(now()) -`last_visit` > '".$this->lasttime."'");
return true;
}// 改后
function session(&$db) {
$this->db = &$db;
session_module_name('user'); //session文件保存方式,这个是必须的!除非在Php.ini文件中设置了
session_set_save_handler(
array(&$this, 'open'), //在运行session_start()时执行 array(&$this, 'close'), //在脚本执行完成或调用session_write_close() 或 session_destroy()时被执行,即在所有session操作完后被执行 array(&$this, 'read'), //在运行session_start()时执行,因为在session_start时,会去read当前session数据
array(&$this, 'write'), //此方法在脚本结束和使用session_write_close()强制提交SESSION数据时执行 array(&$this, 'destroy'), //在运行session_destroy()时执行 array(&$this, 'gc') //执行概率由session.gc_probability 和 session.gc_divisor的值决定,时机是在open,read之后,session_start会相继执行open,read和gc
);
session_start(); //这也是必须的,打开session,必须在session_set_save_handler后面执行
}
@header("content-Type: text/html; charset=UTF-8");
ob_start();//
// include server parameters
require('includes/configure.php');
//include database tables
require('includes/database_table.php');
// set php_self in the local scope
if (!isset($PHP_SELF)) $PHP_SELF = basename($_SERVER['SCRIPT_FILENAME']);
// include the database class
require(DIR_WS_CLASSES . 'db_mysql_class.php');
$db=new DbCom();
// make a connection to the database... now
$db->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE);
$db->query("set names utf8");
require(DIR_WS_FUNCTIONS . 'general.php');
require(DIR_WS_FUNCTIONS . 'html_output.php');
require(DIR_WS_FUNCTIONS . 'common.php');
require(DIR_WS_FUNCTIONS . 'img_common.php');
require(DIR_WS_CLASSES . 'split_page_results.php');
require(DIR_WS_CLASSES . 'comment.class.php');
require(DIR_WS_CLASSES . 'category.class.php');
require(DIR_WS_CLASSES . 'message.class.php');
require(DIR_WS_CLASSES . 'session.class.php');
$session = new session($db);
if($_GET['action']==1){
print_r($_SESSION);exit;//这里输出的是空数组,无语。。
}
$_SESSION['webid']=1;?>
<a href="?action=1">click</a>
<?php
print_r($_COOKIE);//cookie没有影响
echo "<br>";
print_r($_SESSION);//这里能获取,有值?>
sesson前不能有输出,你第一个就header,不行我以前测试过存数据库的
凡是先要用session的
首先要执行session_set_save_handler( )注册相关函数(相关函数要正确)
然后再session_start();如果你的不正确,则不会有结果,你可一步步测试你可以你的构造函数里定义session_set_save_handler( ),前提所有相关函数都能执行
if($_GET['action']==1){ //这是从其它网页上传来的对么?
print_r($_SESSION); //这里输出的是空数组,无语。。
echo 1; //看看 这样能输出1么?
exit;
}
$_SESSION['webid']=1;
我去试试
这个是http协议的里面已经规定好的.
if($_GET['action']==1){
print_r($_SESSION);exit;//这里输出的是空数组,无语。。========类里面的函数应该是有问题的!
}
$_SESSION['webid']=1;?>
<a href="?action=1">click</a>
<?php
print_r($_COOKIE);//cookie没有影响
echo "<br>";
print_r($_SESSION);//这里能获取,有值 =====这里的值是不是1,也就是由上面$_SESSION['webid']=1传的值!
print_r($_SESSION);exit;//这里输出的是空数组,无语。。======= exit;下面得语句不会输出了
}