我有一个文件sess_function.php
此文件列出了用于控制会话的各种(6)函数。
function _open()
{
try{
// Open the database
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} //try
catch(PDOException $e){
echo "Oops, We're experiencing an error CONNECTING.";
file_put_contents('PDODBConnectionErrors.txt', $e->getMessage(), FILE_APPEND);
} //catch
}
## Kill Connection to Mysql (Using PDO)
function _close(){
$dbh = null;
}
## Read a current session
function _read($id){
try{
// Open the database
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// Begin Query
$id = mysql_real_escape_string($id);
$sth = $dbh->prepare("SELECT data FROM sessions WHERE id = '$id'");
$sth->execute();
}
catch(PDOException $e){
echo "Oops, We're experiencing an error. READING";
file_put_contents('PDODBConnectionErrors.txt', $e->getMessage(), FILE_APPEND);
} //catch
## return '';
}
## Write to current session
function _write($id, $data){
// WRITE Vars
try{
// Open the database
## require_once('dbi.php');
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// Begin Query
$access = time();
$id = mysql_real_escape_string($id);
$access = mysql_real_escape_string($access);
$data = mysql_real_escape_string($data);
$sth = $dbh->prepare("REPLACE INTO sessions VALUES ('$id', '$access', '$data')");
$sth->execute();
}
catch(PDOException $e){
echo "Oops, We're experiencing an error. REPLACING SESSION VAL";
file_put_contents('PDODBConnectionErrors.txt', $e->getMessage(), FILE_APPEND);
} //catch
}
## Destroy current session
function _destroy($id){
try{
// Open the database
## require_once('dbi.php');
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// Begin Query
$id = mysql_real_escape_string($id);
$sth = $dbh->prepare("DELETE FROM sessions WHERE id = '$id'");
$sth->execute();
}
catch(PDOException $e){
echo "Oops, We're experiencing an error.";
file_put_contents('PDODBConnectionErrors.txt', $e->getMessage(), FILE_APPEND);
} //catch
}
## Clean old session data out
function _clean($max){
try{
// Open the database
## require_once('dbi.php');
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// Begin Query
$old = time() - $max;
$old = mysql_real_escape_string($old);
$sth = $dbh->prepare("DELETE FROM sessions WHERE access < '$old'");
$sth->execute();
}
catch(PDOException $e){
echo "Oops, We're experiencing an error. DEL FROM SESSION";
file_put_contents('PDODBConnectionErrors.txt', $e->getMessage(), FILE_APPEND);
} //catch
}
和 index.php
////////////////////////////////////////////////////////////////////////////////////
###### Require Database ###### ////////////////////////
####### DB Config Setting #######
$host ='localhost'; //////////////
$dbname ='mydb';//////////
$user ='root'; //////////////
$pass =''; //////////////
/////////////////////////////////
////////////////////////
////////////////////////////////////////////////////////////////////////////////////
###### Start session ////////////////////////
session_start(); ////////////////////////
###### Call Session Functions Include ###### ////////////////////////
require_once('src/cfg/sess_function.php'); ////////////////////////
###### Call function as contained in sess_function() ###### //
session_set_save_handler('_open','_close','_read','_write','_destroy','_clean'); //
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
表格如下:
问题是:它没有给我任何问题,但sessions
表中没有任何内容......
如果我UNCOMMENT session_start();
它会抛出这些警告:
Oops, We're experiencing an error. READING
Warning: session_start() Cannot send session cookie - headers already sent by sess_function.php:33)index.php on line 12
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started sess_function.php:33) in index.php on line 12
任何??
答案 0 :(得分:1)
第一条错误消息是您应该查看的内容。 PHP需要发送HTTP头数据(cookie),但sess_function.php中已有一些输出。常规输出后(例如echo
)无法发送标头数据。你打印“哎呀,我们遇到了错误。阅读”,这是PHP抱怨的输出。删除所有echo语句将解决会话问题。
然后:未设置$dbname
,$user
,$pass
和$host
个变量。 global $dbname, $host, $user, $pass;
声明解决了这个问题。
最后:必须在session_start之前调用session_set_save_handler,否则PHP将不会调用自定义函数。输出已发送错误的原因是在session_start完成运行之前触发了MySQL错误(在 session_start期间发生了MySQL错误)
答案 1 :(得分:0)
为什么你的session_start()被评论了?尝试打开它
您是否正在启动自动会话?