3 回答
TA贡献1895条经验 获得超3个赞
您确实想在创建配置文件之前检查连接,因此一旦创建,您就知道它将起作用
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['db-details']) {
$dbName = $_POST['dbName'];
$dbUsername = $_POST['dbUsername'];
$dbPassword = $_POST['dbPassword'];
$dbHost = $_POST['dbHost'];
// use error supression so you get to process the error rather
// than PHP throwing an error
$connection = @mysqli_connect($_POST['dbHost'],
$_POST['dbUsername'],
$_POST['dbPassword'],
$_POST['dbName']);
if ( ! $connection ) {
// do whatever you need to when the information passed does not work
exit;
}
// must be valid as we connected
$str = '<?php' . PHP_EOL;
$str .= 'define("DB_HOST", "' . $_POST['dbHost'] . '");' . PHP_EOL;
$str .= 'define("DB_USER", "' . $_POST['dbUsername'] . '");' . PHP_EOL;
$str .= 'define("DB_PASS", "' . $_POST['dbPassword'] . '");' . PHP_EOL;
$str .= 'define("DB_NAME", "' . $_POST['dbName'] . '");' . PHP_EOL;
$str .= '$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);' . PHP_EOL;
// create the config file in all confidence that the values will work
file_put_contents($phConfigFile, $str);
}
TA贡献1852条经验 获得超1个赞
这与里格斯弗利的答案相同,但有所改进。
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['db-details'])) {
try {
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connection = new mysqli(
$_POST['dbHost'],
$_POST['dbUsername'],
$_POST['dbPassword'],
$_POST['dbName']
);
$DBconfig = [
'dbHost' => $_POST['dbHost'],
'dbUsername' => $_POST['dbUsername'],
'dbPassword' => $_POST['dbPassword'],
'dbName' => $_POST['dbName']
];
// generate the config file
$contents = '<?php' . PHP_EOL;
$contents .= '$DBconfig = ' . var_export($DBconfig, true) . ';' . PHP_EOL;
$contents .= 'mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);' . PHP_EOL;
$contents .= '$connection = new mysqli(
$DBconfig["dbHost"],
$DBconfig["dbUsername"],
$DBconfig["dbPassword"],
$DBconfig["dbName"]
);';
echo $contents;
} catch (\Exception $e) {
// echo error message here and show the same form.
}
}
基本上,您应该不惜一切代价避免错误抑制运算符。值得庆幸的是,PHP有异常,可以捕获和处理。
如果没有异常,则可以将连接详细信息放在数组中,然后将该数组导出为字符串。生成连接文件并保存。
我要改进的另一件事是将其封装在函数中,以防止配置细节溢出。
// generate the config file
$contents = '<?php' . PHP_EOL;
$contents .= 'function db_connect():\mysqli { '. PHP_EOL;
$contents .= '$DBconfig = ' . var_export($DBconfig, true) . ';' . PHP_EOL;
$contents .= 'mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);' . PHP_EOL;
$contents .= 'return new mysqli(
$DBconfig["dbHost"],
$DBconfig["dbUsername"],
$DBconfig["dbPassword"],
$DBconfig["dbName"]
);';
$contents .= PHP_EOL;
$contents .= '}'.PHP_EOL;
$contents .= '$connection = db_connect();'.PHP_EOL;
echo $contents;
TA贡献1865条经验 获得超7个赞
因此,如果我答对了,您想知道用户输入的数据是否实际上有效以连接到服务器。未验证它。
您可以包含刚刚编写的文件并对其进行测试。
只需确保在包含后使用相同的变量名称即可。
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['db-details']) {
$dbName = $_POST['dbName'];
$dbUsername = $_POST['dbUsername'];
$dbPassword = $_POST['dbPassword'];
$dbHost = $_POST['dbHost'];
$phConfigFile = 'includes/ph_config.php';
file_put_contents($phConfigFile, $phConfigData);
include($phConfigFile);
if ($connection->connect_errno) {
//data for the connection isn't valid
echo "Failed to connect to MySQL: " . $connection->connect_error;
exit();
}
}
- 3 回答
- 0 关注
- 94 浏览
添加回答
举报