3 回答

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊
您確實(shí)想在創(chuàng)建配置文件之前檢查連接,因此一旦創(chuàng)建,您就知道它將起作用
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貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊
這與里格斯弗利的答案相同,但有所改進(jìn)。
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.
}
}
基本上,您應(yīng)該不惜一切代價(jià)避免錯(cuò)誤抑制運(yùn)算符。值得慶幸的是,PHP有異常,可以捕獲和處理。
如果沒有異常,則可以將連接詳細(xì)信息放在數(shù)組中,然后將該數(shù)組導(dǎo)出為字符串。生成連接文件并保存。
我要改進(jìn)的另一件事是將其封裝在函數(shù)中,以防止配置細(xì)節(jié)溢出。
// 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貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
因此,如果我答對(duì)了,您想知道用戶輸入的數(shù)據(jù)是否實(shí)際上有效以連接到服務(wù)器。未驗(yàn)證它。
您可以包含剛剛編寫的文件并對(duì)其進(jìn)行測(cè)試。
只需確保在包含后使用相同的變量名稱即可。
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 關(guān)注
- 124 瀏覽
添加回答
舉報(bào)