gpt4 book ai didi

php - session 不会保存到 mysql

转载 作者:行者123 更新时间:2023-11-29 14:32:32 25 4
gpt4 key购买 nike

我有一个文件 sess_function.php

此文件列出了用于控制 session 的各种 (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'); //
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

该表如下所示: img1

问题是:它没有给我带来任何问题,但是 sessions 表中没有存储任何内容...

如果我取消注释 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

有人吗?

最佳答案

您应该查看第一条错误消息。 PHP 需要发送 HTTP header 数据(cookie),但 sess_function.php 中已经有一些输出。常规输出后无法发送 header 数据(例如 echo)。您打印“哎呀,我们遇到错误。正在阅读”,这是 PHP 提示的输出。删除所有 echo 语句将解决 session 问题。

然后:$dbname$user$pass$host 变量未设置。 global $dbname, $host, $user, $pass; 声明修复了这个问题。

最后:session_set_save_handler必须在session_start之前调用,否则PHP将不会调用自定义函数。发生输出已发送错误是因为在 session_start 完成运行之前触发了 MySQL 错误(MySQL 错误发生在 session_start 期间)

关于php - session 不会保存到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9731018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com