在哪里放置数据库连接设置? [英] Where to put database connection settings?

查看:51
本文介绍了在哪里放置数据库连接设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将数据库连接的连接设置放在何处(主机,dbname,用户,密码之类的东西)?是将它放置在数据库类或文件中,还是放置在配置文件中的外部,还是其他地方?

Where do you put the connection settings for a database connection (things like host, dbname, user, password)? Is it placed in the database class or file, or outside in a config file, or somewhere else?

推荐答案

理想情况下,您应该将其放置在配置文件中,该文件可以像PHP数组一样简单.

Ideally, you should place it in a config file, which can be something as simple as a PHP array.

例如:db_config.php

For example: db_config.php

$db_config = array(
  'host' => 'localhost',
  'user' => 'username',
  'password' => 'qwerty'
);

然后应将此文件放置在文档根目录之外,以实现最大的安全性.这样,如果您的虚拟主机使您失败,并开始将PHP文件作为文本文件提供服务(偶然发生),那么没人会获得您的数据库凭据.

You should then place this file outside your document root for maximum security. This way, if your webhost fails you and begins serving PHP files as text files (happens), no one can get your DB credentials.

这篇关于在哪里放置数据库连接设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆