【问题标题】:PHP is generating multiple sessionsPHP 正在生成多个会话
【发布时间】:2013-04-09 22:00:26
【问题描述】:

我不确定这里出了什么问题,但我已经尝试了所有方法来解决一个非常烦人的问题,这个问题现在一直困扰着我的网站,那就是 PHP 为每个用户生成多个会话。

目前,我是唯一使用该站点的人,所以我完全知道其他人不可能创建会话,但是,可以肯定的是,我在启动 session_start() 后立即存储了 IP 地址我的会话类.. 并且低看,这是我的 IP 地址不断生成。登录后,数据库中又添加了 5 或 6 条记录,每条记录都包含我的 IP 地址,只有一条包含会话信息。

现在,网站运行良好,但我知道这是不正常的行为。我只调用 session_start() 一次,除了登录之外,我没有在站点上的任何地方调用 regenerate_id,但是即使在登录之前,只要我访问该站点,就会添加三个记录。

我想知道这是否是一个已知问题,以及是否有人可以梳理我的代码,看看他们是否能看到任何可能导致这种奇怪行为的东西,因为不幸的是,谷歌和搜索 SO 上的一个小时左右'没有出现很多结果(除了保存问题的未回答问题)

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 文件有问题吗?谢谢:)

【问题讨论】:

    标签: php session


    【解决方案1】:

    你检查过 PHP INI 中的 session.auto_start 吗?

    【讨论】:

      猜你喜欢
      • 2019-03-30
      • 1970-01-01
      • 2010-10-25
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多