note 89019 added to class.pdo

From: Date: Wed, 18 Feb 2009 15:03:48 +0000
Subject: note 89019 added to class.pdo
Groups: php.notes 
Request: Send a blank email to php-notes+get-150803@lists.php.net to get a copy of this message
"And storing username/password inside class is not a very good idea for production code." Good idea is to store database connection settings in *.ini files but you have to restrict access to them. For example this way: my_setting.ini: [database] driver = mysql host = localhost ;port = 3306 schema = db_schema username = user password = secret Database connection: <?php class MyPDO extends PDO { public function __construct($file = 'my_setting.ini') { if (!$settings = parse_ini_file($file, TRUE)) throw new exception('Unable to open ' . $file . '.'); $dns = $settings['database']['driver'] . ':host=' . $settings['database']['host'] . ((!empty($settings['database']['port'])) ? (';port=' . $settings['database']['port']) : '') . ';dbname=' . $settings['database']['schema']; parent::__construct($dns, $settings['database']['username'], $settings['database']['password']); } } ?> Database connection parameters are accessible via human readable ini file for those who screams even if they see one PHP/HTML/any_other command. ---- Server IP: 147.32.160.17 Probable Submitter: 147.229.217.1 ---- Manual Page -- http://www.php.net/manual/en/class.pdo.php Edit -- https://master.php.net/note/edit/89019 Del: integrated -- https://master.php.net/note/delete/89019/integrated Del: useless -- https://master.php.net/note/delete/89019/useless Del: bad code -- https://master.php.net/note/delete/89019/bad+code Del: spam -- https://master.php.net/note/delete/89019/spam Del: non-english -- https://master.php.net/note/delete/89019/non-english Del: in docs -- https://master.php.net/note/delete/89019/in+docs Del: other reasons-- https://master.php.net/note/delete/89019 Reject -- https://master.php.net/note/reject/89019 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#150803) next »