使用 define
存储 mysql PDO 连接有哪些缺点?例如,
define( 'DB_HOST', 'localhost' );
define( 'DB_USER', 'root' );
define( 'DB_PASS', 'xxx' );
define( 'DB_NAME', 'xxx' );
define( 'DSN', 'mysql:host='.DB_HOST.';dbname='.DB_NAME );
我一直在考虑使用 const
就像 this example但它也有局限性。
例如,
- 我无法使用
const
执行'mysql:host='.DB_HOST.';dbname='.DB_NAME
。 - 我也无法使用 const 执行以下操作,
(其他配置信息...)
define ( 'WEBSITE_DOCROOT', str_replace( '\\', '/', dirname( __FILE__ ) ).'/' );
define( 'SCHEME', isset( $_SERVER['HTTPS'] ) ? 'https://' : 'http://' );
define
似乎符合目的,但我不确定它有多安全,以及使用 Define 存储设置信息时应该注意什么?有什么建议吗?
另一种选择是将它们存储在一个类中,
$config = array(
'DB_USER' => 'root',
'DB_PW' => 'password'
);
但据说使用此选项,最终用户可以修改文件并破坏整个应用程序
(摘自 here )。用户如何修改文件?
最佳答案
您定义
的任何内容都将可供您的 PHP 代码的所有部分使用,这可能会带来安全风险。如果您使用变量,则可以限制数据的公开,因为变量的范围有限。
But there are said with this option, the end user can screw the file and break the entire app(taken from here). How the user can screw the file?
这篇文章并不是从网站用户的角度来谈论用户,而是作为代码本身的用户(例如,使用开源项目并编辑配置的人)。
关于PHP 使用定义存储 mysql PDO 连接的缺点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28924547/