note 94100 deleted from pdo.connections by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:55:44 +0000 |
| Subject: | note 94100 deleted from pdo.connections by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215378@lists.php.net to get a copy of this message | ||
Note Submitter: neville at whitespacers dot com
----
To avoid exposing your connection details should you fail to remember to catch any exception thrown
by the PDO constructor you can use the following class to implicitly change the exception handler
temporarily.
<?php
Class SafePDO extends PDO {
public static function exception_handler($exception) {
// Output the exception details
die('Uncaught exception: ', $exception->getMessage());
}
public function __construct($dsn, $username='', $password='',
$driver_options=array()) {
// Temporarily change the PHP exception handler while we . . .
set_exception_handler(array(__CLASS__, 'exception_handler'));
// . . . create a PDO object
parent::__construct($dsn, $username, $password, $driver_options);
// Change the exception handler back to whatever it was before
restore_exception_handler();
}
}
// Connect to the database with defined constants
$dbh = new SafePDO(PDO_DSN, PDO_USER, PDO_PASSWORD);
?>