【问题标题】:Need to close database connection for session需要关闭会话的数据库连接
【发布时间】:2013-06-02 16:05:45
【问题描述】:

我有以下代码,基本上是从http://www.spiration.co.uk/post/1333/PHP-5-sessions-in-mysql-database-with-PDO-db-objects提炼出来的

让我觉得有点奇怪的是,里面没有任何东西可以关闭数据库连接(即设置 $this->db = null)。我应该为此担心(或这里的其他任何事情)吗?具体来说,我应该把 $this->db = null 放在 close() 函数中还是 close() 这里有别的意思? :)

public $db;
public $maxlifetime = 1800; /* 30 mins */
public $expiry;

public function __destruct(){
session_write_close();
}

public function open( $path, $name ) {
$this->db = new PDO('mysql:host=' . MySQLConfigClass::MySql_SERVERNAME . ';dbname=' . MySQLConfigClass::MySql_DBNAME, MySQLConfigClass::MySql_LOGINNAME, MySQLConfigClass::MySql_PASS);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return true;
}

public function close() {
return true;
}

public function read($se_id){
$qry = "select se_value from sessions where se_id = '$se_id' and se_expires > " . time();
$sth = $this->db->prepare($qry);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
return $result['se_value'];
}

public function write($se_id, $se_val){
$this->expiry = time() + $this->maxlifetime;
try {
$qry= "insert into sessions (se_id, se_value, se_expires) values('$se_id', '$se_val', $this->expiry)";
$sth = $this->db->prepare($qry);
$sth->execute();
} catch (PDOException $e) {
$qry= "update sessions set se_value='$se_val', se_expires=$this->expiry where se_id='$se_id'";
$sth = $this->db->prepare($qry);

$sth->execute();
}
}

public function destroy($se_id){
$qry = "delete from sessions where se_id ='$se_id'";
$sth = $this->db->prepare($qry);
$tot= $sth->execute();
return ($tot);
}

public function gc($maxlifetime){
$qry = "delete from sessions where se_expires < ".time();
$sth = $this->db->prepare($qry);
$tot= $sth->execute();
return ($tot);
}
}

$session = new Session;
session_set_save_handler(
array(&$session, "open"),
array(&$session, "close"),
array(&$session, "read"),
array(&$session, "write"),
array(&$session, "destroy"),
array(&$session, "gc")
);

session_start();

【问题讨论】:

    标签: php mysql session pdo


    【解决方案1】:

    在 PDO 中,一旦数据库对象(由new PDO() 创建)被销毁,数据库连接就会自动关闭。请参阅 PHP PDO 文档中的 Connections and connection management

    但是,如果您在创建 PDO 对象时设置 PDO::ATTR_PERSISTENT =&gt; true,则连接将被缓存并在后续页面加载时重用,前提是它们使用相同的数据库凭据。

    【讨论】:

    • 这里有没有真正破坏对象的地方?
    • 您发布的代码只是一个包装类,因此当您从中实例化的对象超出范围时,它将被销毁。
    • 如果设置 PDO::ATTR_PERSISTENT => true,这仍然成立吗?
    • 这不在您的原始问题中,但无论如何我将其添加到我的答案中。享受:)
    • 确实如此,所以我会接受你的回答——尽管我会更进一步地感谢你的洞察力。如果使用持久连接,我应该在此处将 $this->dbh = null 添加到 close() 方法吗?如果没有,我应该在哪里/如何?
    【解决方案2】:

    当您删除对 PDO 对象的所有引用时(例如通过将引用它的变量设置为 null),连接将关闭。当 PHP 退出时也会发生同样的事情。您无需做任何事情。

    此外,您可能存在一些严重的 SQL 注入漏洞!您已经在使用准备好的查询...确保为任何可变数据使用参数。

    【讨论】:

    • 谢谢 - 这是关于参数的一个好点,会改变它...我知道将变量设置为 null 将关闭连接,我担心它没有在任何地方设置为 null 跨度>
    • @davidkomer,不必如此。一旦脚本完成执行,就会发生垃圾收集并断开连接。你的脚本很好。没什么好担心的。
    猜你喜欢
    • 2011-07-29
    • 1970-01-01
    • 2017-03-19
    • 2019-07-03
    • 2016-01-18
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多