0
我不确定这里有什么错,但我已经尝试从字面上解决一个非常烦人的问题,这个问题一直困扰着我的网站,那就是PHP为每个用户生成多个会话。PHP正在生成多个会话
目前,我是唯一使用该站点的人,所以我完全清楚不会有其他人创建会话的可能性,但是,确保在启动session_start()后立即存储IP地址,在我的会话类..低,看,这是我的IP地址不断生成。登录后,会有5到6条记录添加到数据库中,每条记录都包含我的IP地址,只有一条记录会话信息。
现在,网站工作正常,但我知道这是不正常的行为。我只调用了session_start()一次,除了登录之外,我没有在网站的任何地方调用regenerate_id,但是即使在我访问该网站之后立即添加了三条记录之前,也是如此。
我在想,如果这是一个已知问题,并且任何人都可以梳理我的代码,看看他们是否能看到任何可能导致这种奇怪行为的东西,因为不幸的是,还没有(从与保存问题悬而未决的问题分开)
class Sessions {
private $life_time, $database;
public function __construct()
{
$this->life_time = DatabaseConfig::read('SESSION_MAX_LIFETIME'); // Set within a config file, returns 1440
$this->database = DatabaseCore::getInstance(); // Basically allows me to create only one instance of the database to avoid overhead from multiple persistent connections.
session_set_save_handler(
array(&$this, "NewSession"),
array(&$this, "CloseSession"),
array(&$this, "ReadSession"),
array(&$this, "WriteSession"),
array(&$this, "DestroySession"),
array(&$this, "CleanSessions")
);
session_name("SESSION_GEEK_PANEL");
session_start();
}
public function NewSession($save_path, $session_name) {
$sess_save_path = $save_path;
return true;
}
public function ReadSession($id) {
$sth = $this->database->dbh->prepare("SELECT `session_data` FROM `sessions` WHERE `session_id` = :session_id ");
$sth->execute(array(
':session_id' => $id
));
if($sth->rowCount() > 0)
{
$row = $sth->fetch();
$data = $row['session_data'];
}
return $data;
}
public function WriteSession($id, $data) {
$sth = $this->database->dbh->prepare("REPLACE `sessions` (`session_id`, `session_data`, `expires`) VALUES(:id, :data, :expires)");
$sth->execute(array(
':id' => $id,
':data' => $data,
':expires' => time() + $this->life_time
));
return true;
}
public function DestroySession($id) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
$sth = $this->database->dbh->prepare("DELETE FROM `sessions` WHERE `session_id` = :id");
$sth->execute(array(
':id' => $id
));
return true;
}
public function CleanSessions() {
$sth = $this->database->dbh->prepare("DELETE FROM `sessions` WHERE `expires` < :time");
$sth->execute(array(
':time' => time()
));
return true;
}
public function CloseSession() {
return true;
}
}
DatabaseConfig::write('SESSION_MAX_LIFETIME', MAX_SESSION_LIFE);
所有帮助是极大的赞赏冒出了很多结果,终于..可这是与主机或PHP INI文件中的错吗?谢谢:)
是的,这是我原来的想法,它被设置为0 /关 – 2013-04-09 22:09:52