更改 php/mysql 数据库会创建不安全的连接吗?
Will changing the php/mysql database create an insecure connection?
或者:"Will changing the php/mysql database in this manner create an insecure connection?"
我正在考虑自动化我的 live/test 数据库。我有点 mysql 天真,所以我想我最好在这里问这个问题:
在以下 php/mysql 场景中 and/or 安全问题会产生什么后果?
// set up the standard database
// Connection codes:
$host="localhost";
$user="imauser";
$password="imapassword";
$dbname="liveDB";
$cxn = mysqli_connect ($host,$user,$password,$dbname)
or die ("Couldn't connect to the server.");
// check if $testMode is active, and reset $cxn with a new (test) $dbname:
if($testMode == TRUE){
$dbname="testDB"; // test database
// reset the cxn:
$cxn = mysqli_connect ($host,$user,$password,$dbname)
or die ("Couldn't connect to the server.");
}
这将允许我在代码中的更高级别切换 $testMode。 $cxn 的简单覆盖是否有效,或者我是否有一个开放且活动的 mysqli_connect 连接悬而未决?
我的意思是,只要您记得更改测试模式变量,就不会出现安全或其他问题。虽然我会用这样的 switch 语句来做到这一点
$devmode = "TEST";
$conn = null;
switch($devmode) {
case "TEST"
//conn here
break;
//case dev
default:
//local host con or prod conn
break;
}
虽然有更好的方法可以做到这一点,但我强烈建议您查看诸如 doctrine 之类的东西来为您管理所有 SQL,在 doctrine 中,您可以轻松地交换连接及其独立的数据库类型。
我没有在您的代码中看到任何我认为不安全的内容。但是,mysqli_connect()
两次似乎没有必要。
您可以为此创建一个简单的 ternary;
// set up the standard database
// Connection codes:
$host = "localhost";
$user = "imauser";
$password = "imapassword";
//use a ternary like this
$dbname = $testMode ? 'testDB' : 'liveDB';
$cxn = mysqli_connect($host, $user, $password, $dbname) or die("Couldn't connect to the server: " . mysqli_connect_errno());
说明
上面代码中的三进制等价于:
if($testMode == true) {
$dbname = 'testDB';
} else {
$dbname = 'liveDB';
}
三元可以这样简单解释$variable = CONDITION ? TRUE : FALSE
如果你有一个布尔变量(比如$testMode
);你可以通过直接检查它作为条件来检查它是真还是假。
if($testMode)
等同于 if($testMode == true)
.
其他变化
- 我更改了你的
die()
调用以实际显示错误(如果它不能)
连接。
- 更改了一些格式以使其更易于阅读。
最好将数据库凭据与代码分开。以防万一有人找到阅读您代码的方法,他们不应看到您的数据库密码。
将数据库凭据保存在配置文件中,您的应用程序会在启动时读取该文件。我会使用 parse_ini_file().
这是一个示例配置文件:
[database]
host=localhost
user=imauser
password=imapassword
dbname=liveDB
阅读方式如下:
$config = parse_ini_file('config.ini', true);
如果我输出 print_r($config)
,我会看到:
Array
(
[database] => Array
(
[host] => localhost
[user] => imauser
[password] => imapassword
[dbname] => liveDB
)
)
这样你就可以在测试和生产环境中部署相同的代码,你只需要替换测试和生产服务器中的配置文件。
注意:确保您没有将配置文件放在 Web 服务器可以从中提供文件的目录下。您的 PHP 代码可以从服务器上的任何位置读取文件,因此请确保没有人可以简单地在浏览器中打开配置文件。
或者:"Will changing the php/mysql database in this manner create an insecure connection?"
我正在考虑自动化我的 live/test 数据库。我有点 mysql 天真,所以我想我最好在这里问这个问题:
在以下 php/mysql 场景中 and/or 安全问题会产生什么后果?
// set up the standard database
// Connection codes:
$host="localhost";
$user="imauser";
$password="imapassword";
$dbname="liveDB";
$cxn = mysqli_connect ($host,$user,$password,$dbname)
or die ("Couldn't connect to the server.");
// check if $testMode is active, and reset $cxn with a new (test) $dbname:
if($testMode == TRUE){
$dbname="testDB"; // test database
// reset the cxn:
$cxn = mysqli_connect ($host,$user,$password,$dbname)
or die ("Couldn't connect to the server.");
}
这将允许我在代码中的更高级别切换 $testMode。 $cxn 的简单覆盖是否有效,或者我是否有一个开放且活动的 mysqli_connect 连接悬而未决?
我的意思是,只要您记得更改测试模式变量,就不会出现安全或其他问题。虽然我会用这样的 switch 语句来做到这一点
$devmode = "TEST";
$conn = null;
switch($devmode) {
case "TEST"
//conn here
break;
//case dev
default:
//local host con or prod conn
break;
}
虽然有更好的方法可以做到这一点,但我强烈建议您查看诸如 doctrine 之类的东西来为您管理所有 SQL,在 doctrine 中,您可以轻松地交换连接及其独立的数据库类型。
我没有在您的代码中看到任何我认为不安全的内容。但是,mysqli_connect()
两次似乎没有必要。
您可以为此创建一个简单的 ternary;
// set up the standard database
// Connection codes:
$host = "localhost";
$user = "imauser";
$password = "imapassword";
//use a ternary like this
$dbname = $testMode ? 'testDB' : 'liveDB';
$cxn = mysqli_connect($host, $user, $password, $dbname) or die("Couldn't connect to the server: " . mysqli_connect_errno());
说明
上面代码中的三进制等价于:
if($testMode == true) {
$dbname = 'testDB';
} else {
$dbname = 'liveDB';
}
三元可以这样简单解释$variable = CONDITION ? TRUE : FALSE
如果你有一个布尔变量(比如$testMode
);你可以通过直接检查它作为条件来检查它是真还是假。
if($testMode)
等同于 if($testMode == true)
.
其他变化
- 我更改了你的
die()
调用以实际显示错误(如果它不能) 连接。 - 更改了一些格式以使其更易于阅读。
最好将数据库凭据与代码分开。以防万一有人找到阅读您代码的方法,他们不应看到您的数据库密码。
将数据库凭据保存在配置文件中,您的应用程序会在启动时读取该文件。我会使用 parse_ini_file().
这是一个示例配置文件:
[database]
host=localhost
user=imauser
password=imapassword
dbname=liveDB
阅读方式如下:
$config = parse_ini_file('config.ini', true);
如果我输出 print_r($config)
,我会看到:
Array
(
[database] => Array
(
[host] => localhost
[user] => imauser
[password] => imapassword
[dbname] => liveDB
)
)
这样你就可以在测试和生产环境中部署相同的代码,你只需要替换测试和生产服务器中的配置文件。
注意:确保您没有将配置文件放在 Web 服务器可以从中提供文件的目录下。您的 PHP 代码可以从服务器上的任何位置读取文件,因此请确保没有人可以简单地在浏览器中打开配置文件。